美文网首页基础知识
java 绝学 之 化骨绵掌-stream(1)

java 绝学 之 化骨绵掌-stream(1)

作者: MikeLi666 | 来源:发表于2018-11-29 17:06 被阅读50次

学习java如同学习武功,代码规范就是武功招式,设计模式如同内功心法。如果你会几个好用的开源工具,就如同学了几本武学秘籍。秘籍虽然好,但是如果没有用对,分分钟反伤自己。

化骨绵掌外柔内刚,以爆发劲为主。手法以掌为主,运转舒展,动作连绵不断,掌法运行成环;劲力内蓄刚劲,外现绵柔,爆发迅猛。
stream就是java中的化骨绵掌。一套连续不断地输出,帮我们攻击拉满。

让我们通过一个具体的小任务来看看,化骨绵掌-stream和普通招式对比。
我现在需要将integerList中的所有数字都取平方,并且输出到resultList

普通招式1 ⭐️⭐️⭐️

public class Test20181129 {

    List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);

    @Test
    public void test1() {

        //用于存放结果
        List<Integer> resultList = new LinkedList<>();

        //循环调用函数
        for (int i = 0; i < integerList.size(); i++) {
            resultList.add(fun(integerList.get(i)));
        }

        //输出结果
        System.out.println(resultList);

    }

    private Integer fun(Integer i) {
        return i * i;
    }


}
[1, 4, 9, 16, 25]

普通招式2 ⭐️⭐️⭐️

public class Test20181129 {

    List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);

    @Test
    public void test2() {

        //用于存放结果
        List<Integer> resultList = new LinkedList<>();

        //循环调用函数
        for (Integer integer :integerList) {
            resultList.add(fun(integer));
        }

        //输出结果
        System.out.println(resultList);

    }

    private Integer fun(Integer i) {
        return i * i;
    }

}
[1, 4, 9, 16, 25]

化骨绵掌-stream ⭐️⭐️⭐️⭐️⭐️

public class Test20181129 {

    List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);

    @Test
    public void test3() {

        List<Integer> result = integerList.parallelStream().
                map(integer -> integer * integer).
                collect(Collectors.toList());
        System.out.println(result);

    }

}
[1, 4, 9, 16, 25]

好像没有很厉害嘛,仅仅只是省了几行代码而已。

那我们现在让fun函数需要消耗点时间,然后看看不同的方法都要用多少时间?

普通招式1 节省时间 ⭐️

public class Test20181129 {

    List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);

    @Test
    public void test1() {

        long begin = System.currentTimeMillis();

        //用于存放结果
        List<Integer> resultList = new LinkedList<>();

        //循环调用函数
        for (int i = 0; i < integerList.size(); i++) {
            resultList.add(fun(integerList.get(i)));
        }

        long end = System.currentTimeMillis();

        System.out.println("useTime: "+(end-begin));
        //输出结果
        System.out.println(resultList);

    }

    private Integer fun(Integer i) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return i * i;
    }
}
useTime: 5015
[1, 4, 9, 16, 25]

普通招式2 节省时间 ⭐️

public class Test20181129 {

    List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);

    @Test
    public void test1() {

        long begin = System.currentTimeMillis();

        //用于存放结果
        List<Integer> resultList = new LinkedList<>();

        //循环调用函数
        for (Integer integer :integerList) {
            resultList.add(fun(integer));
        }

        long end = System.currentTimeMillis();

        System.out.println("useTime: "+(end-begin));
        //输出结果
        System.out.println(resultList);

    }

    private Integer fun(Integer i) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return i * i;
    }
}
useTime: 5009
[1, 4, 9, 16, 25]

化骨绵掌-stream 节省时间 ⭐️⭐️⭐️⭐️⭐️

public class Test20181129 {

    List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);

    @Test
    public void test3() {

        long begin = System.currentTimeMillis();

        List<Integer> result = integerList.parallelStream().
                map(integer -> fun(integer)).
                collect(Collectors.toList());

        long end = System.currentTimeMillis();
        System.out.println(result);
        System.out.println("useTime : "+(end-begin));
    }

    public Integer fun(Integer integer)  {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return integer * integer;
    }
}
useTime : 1087
[1, 4, 9, 16, 25]

在造成相同伤害的基础上(结果一致),化骨绵掌-stream的执行时间只用了 普通招式1和2 的1/5
我们并没有显式的去开多线程,但是通过stream这一切就精巧的做完了。
这就是stream的魅力,接下来我会带着大家一起看看stream都能帮我们做什么?以及如何避免反伤我们自己。

相关文章

  • java 绝学 之 化骨绵掌-stream(1)

    学习java如同学习武功,代码规范就是武功招式,设计模式如同内功心法。如果你会几个好用的开源工具,就如同学了几本武...

  • java 绝学 之 化骨绵掌-stream(2)

    对于使用 化骨绵掌-stream,主要是三个步骤. 1获取内力 2运功(内力产生一系列变化) 3发力(造成伤害) ...

  • java8-stream 3 常用中间步骤(费大劲做了gif)

    这次呢,主要来讲讲使用 化骨绵掌-stream 中和核心部分 运功。 本节中,我将Stream类中的返回值类型还是...

  • 化骨绵掌

    “大师,救命呀,快救救我。”方成苦苦的哀求,“我中毒好几年了,眼看着生命就快结束了。我不想这么早就去极乐世界,快救...

  • 化骨绵掌

  • 化骨绵掌修炼手册

    上周六晚上,我有幸参加了一个学习训练营的结业分享环节,总结回顾了我在21天内学到的知识,颠覆的观点和形成的变化。 ...

  • 摧花辣手与化骨绵掌

    自认为是个爱花爱草的人。也喜欢侍候(其实摆弄更合适)花花草草。 但是孩儿她爹却把我叫做摧花辣手。他说:人家养花越养...

  • 谢娜女神 化骨绵掌

    化骨绵掌 作词:唐恬 作曲:吴梦奇 演唱:谢娜 大刀有点狠 铁锤又太笨 双节棍打到自己好疼 暗器无声 我扔扔扔 扔...

  • 化骨绵

    我觉得完美受害者可能包括已经采取了种种周折的办法依然失败。索性痛快解决,以卵击石也无所谓。这并非自以为是的不屑,而...

  • Java8之Stream流(六)收集

    Java8之Stream流(一)基础体验 Java8之Stream流(二)关键知识点 Java8之Stream...

网友评论

    本文标题:java 绝学 之 化骨绵掌-stream(1)

    本文链接:https://www.haomeiwen.com/subject/wbyfcqtx.html