Java8新特性系列--流处理

之前博文有关于Java8新特性介绍,但是内容过于简单,实例太少,遂准备做一个Java8新特性系列,目的是和大家一起分享学习java8的乐趣,提升工作效率。这个系列是流处理系列,我会以实际案例由浅及深介绍流处理的使用。

Stream简介

Java8中的Stream是对与集合对象有所加强的新特性,专注于集合对象进行各种非常便利,高效的聚合操作,同时提供串行与并行两种模式的汇聚操作,使用了fork/join并行方式来拆分任务、加速处理过程。注意,这里的stream与原先的文件I/O流没有必然关系,是在Java8中的新内容。

Stream有时类似于一个迭代器,但是相比原来的Iterator串行命令式的执行过程,stream通过并行方式去便利,遍历时stream数据会被分成多段,其中每一段都在不同的线程中进行处理,然后归并统一。

 List<String> list = new ArrayList<>();
 Stream<String> stream = list.stream();// 取流方式

准备

现提供一个简单的pojo类-Person.class

class Person{
        String  name;
        String  com;
        Integer age;
        Integer house;

        public Person(String name, String com, Integer age, Integer house) {
            this.name = name;
            this.com = com;
            this.age = age;
            this.house = house;
        }

        public Integer getHouse() {
            return house;
        }

        public void setHouse(Integer house) {
            this.house = house;
        }

        public Person() {
        }

        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        public Person(String name, String com, Integer age) {
            this.name = name;
            this.com = com;
            this.age = age;
        }

        public String getCom() {
            return com;
        }

        public void setCom(String com) {
            this.com = com;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            DecryptData.Person person = (DecryptData.Person) o;
            return Objects.equals(name, person.name) &&
                    Objects.equals(age, person.age);
        }

        @Override
        public int hashCode() {

            return Objects.hash(name, age);
        }
    }

PK 传统方式vs新特性

简单介绍后,想必也很难理解概念性的东西,我们一步一步做简单的案例对比传统方式和新特性方式
1.集合遍历

  @Test // 遍历集合
    public void testName() throws Exception {

        List<Person> peoples = new ArrayList<>(Arrays.asList(
                new Person("name1",12),
                new Person("name2",13),
                new Person("name2",14),
                new Person("name3",15)
                ));
        // 传统增强for
        for (Person people : peoples) {
            System.out.println(people.getName());
        }
        // 新特性ForEach
        peoples.forEach(people-> System.out.println(people.getName()));

    }

2.查找年龄大于18的人员

  @Test
    public void filterAge() throws Exception {

        List<Person> peoples = new ArrayList<>(Arrays.asList(
                new Person("name1",12),
                new Person("name2",13),
                new Person("name3",33),
                new Person("name4",21),
                new Person("name1",22),
                new Person("name2",15)
        ));
        // 传统过滤
        List<Person> ps=new ArrayList<>();
        for (Person people : peoples) {
            if (people.getAge()>18){
                ps.add(people);
            }
        }
        System.err.println(JSON.toJSONString(ps));
        // 新特性过滤
        List<Person> collect = peoples.stream().filter(person -> person.getAge() > 18).collect(toList());
        System.err.println(JSON.toJSONString(collect));

    }

结果:

[{"age":33,"name":"name3"},{"age":21,"name":"name4"},{"age":22,"name":"name1"}]
[{"age":33,"name":"name3"},{"age":21,"name":"name4"},{"age":22,"name":"name1"}]

传统方式需要我们去遍历判断代码量很多,流处理方式直接使用过滤器filter,像查询数据库一样对查询过滤。Stream完胜!

  1. 过滤获取属性
    有时候我只想要获取姓名的集合并不想要所有person对象,那么在pk一下
  @Test
    public void filterFailed() throws Exception {

        List<Person> peoples = new ArrayList<>(Arrays.asList(
                new Person("name1",12),
                new Person("name2",13),
                new Person("name3",33),
                new Person("name4",21),
                new Person("name1",22),
                new Person("name2",15)
        ));
        // 传统增强for
        List<String> names=new ArrayList<>();
        for (Person people : peoples) {
                names.add(people.getName());
        }
        System.err.println(JSON.toJSONString(names));
        // 新特性ForEach
        List<String> collect = peoples.stream().map(Person::getName).collect(toList());
        System.err.println(JSON.toJSONString(collect));

    }

结果:

["name1","name2","name3","name4","name1","name2"]
["name1","name2","name3","name4","name1","name2"]

同filter一样的道理,在代码量上可是减少了很多的代码。Stream胜出!
4.分组统计
那么问题来了,就上面几种优势也不是很明显啊,那么我们就来再看看两者的差距

 @Test
    public void statistic() throws Exception {
        // 统计每个人房子数量
        List<String> list = new ArrayList<>();
        Stream<String> stream = list.stream();

        List<Person> peoples = new ArrayList<>(Arrays.asList(
                new Person("思聪","杭州",19,50),
                new Person("马云","北京",50,100),
                new Person("思聪","北京",19,20),
                new Person("温州大婶","西安",1,120),
                new Person("温州大婶","杭州",1,100),
                new Person("我",null,18,0),
                new Person("温州大婶","新西兰",1,200)
        ));
        // 传统增强for 统计
        List<Integer> sum =new ArrayList<>();
        for (Person people : peoples) {
               //人生如此艰难,我还要继续。。。此处省略
        }
        System.err.println(JSON.toJSONString(sum));

        // 新特性ForEach
        Map<String, IntSummaryStatistics>  map=  peoples.stream().collect(groupingBy(Person::getName,summarizingInt(Person::getHouse)));

        System.err.println(JSON.toJSONString(map));

    }

结果 :


image.png

没有对比哪来的伤害啊,传统敲代码的我完败。。。。

  1. 坑点
    注意:流操作一个流只能进行一次处理操作,像下面这个的做法就会出现问题。
   List<Person> peoples = new ArrayList<>(Arrays.asList(
                new Person("思聪","杭州",19,50),
                new Person("马云","北京",50,100),
                new Person("思聪","北京",19,20),
                new Person("温州大婶","西安",1,120),
                new Person("温州大婶","杭州",1,100),
                new Person("我",null,18,0),
                new Person("温州大婶","新西兰",1,200)
        ));

        //+++++ 坑点+++++
        Stream<Person> stream = peoples.stream();
        List<String> collect = stream.map(Person::getName).collect(toList()); // 第一次使用 stream
        List<Person> collect1 = stream.filter(person -> person.getAge() > 18).collect(toList());// 第二次使用 stream

        System.out.println();

异常:
java.lang.IllegalStateException: stream has already been operated upon or closed
解决方式:
我终于找到新特性的bug啦,别急,有解决方案:

   @Test
    public void testMyErr() throws Exception {
        List<Person> peoples = new ArrayList<>(Arrays.asList(
                new Person("思聪","杭州",19,50),
                new Person("马云","北京",50,100),
                new Person("思聪","北京",19,20),
                new Person("温州大婶","西安",1,120),
                new Person("温州大婶","杭州",1,100),
                new Person("我",null,18,0),
                new Person("温州大婶","新西兰",1,200)
        ));

        //+++++ 坑点+++++
//        Stream<Person> stream = peoples.stream();
//        List<String> collect = stream.map(Person::getName).collect(toList()); // 第一次使用 stream
//        List<Person> collect1 = stream.filter(person -> person.getAge() > 18).collect(toList());// 第二次使用 stream
//
        // 类似于一个stream的池子,用的时候取,每次都是个新的对象
        Supplier<Stream<Person>> streamSupplier = peoples::stream;
        List<String> collect3 = streamSupplier.get().map(Person::getName).collect(toList()); // 第一次使用 stream
        List<Person> collect4 =streamSupplier.get().filter(person -> person.getAge() > 18).collect(toList());// 第二次使用 stream
        System.out.println();
    }

6.再补充
前面由于仓促,有些不常用的用法没有提到,这里做出补充。

 // 过滤 filter
        List<Person> collect11 = streamSupplier.get().filter(person ->person.getAge() >= 22).collect(toList());
        System.out.println(collect11);

        // 选择字段过滤 map,这里可以在函数块中返回需要的数据
        List<String> collect1 = streamSupplier.get().map(Person::getName).collect(toList());
        List<String> collect11111 = streamSupplier.get().map(c->{
            System.out.println("1111");
            return c.getName() ;
        }).collect(toList());

        // 去重复,也比较常用,但是需要重写eques 和hashcode 的方法
        List<Person> collect2 = streamSupplier.get().distinct().collect(toList());

        // 限制条数,做分页可以使用
        List<Person> collect3 = streamSupplier.get().limit(4).collect(toList());

        // 统计数量
        long count = stream.filter(p -> p.getAge() > 20).count();

        // 扁平化
        List<String[]> collect4 = Arrays.stream(strs).map(s -> s.split("")).distinct().collect(toList());
        List<String[]> collect5 = Arrays.stream(strs).map(s -> s.split("a")).distinct().collect(toList());

        // 终端操作

        // 规约

        Optional<Integer> reduce = streamSupplier.get().filter(s -> s.getAge() < 30).map(Person::getAge).reduce(Integer::sum);
        Integer reduce1 = streamSupplier.get().filter(s -> s.getAge() < 40).map(Person::getAge).reduce(0,(a,b)->a+b);
        Integer reduce2 = streamSupplier.get().filter(s -> s.getAge() < 40).map(Person::getAge).reduce(0,(a,b)->a+b);
        Integer reduce3 = streamSupplier.get().filter(s -> s.getAge() < 40).map(Person::getAge).reduce(2,(a,b)->a+b);

输出结果:

简单过滤结果
[{"age":22,"name":"name2"},{"age":33,"name":"name3"},{"age":44,"name":"name4"},{"age":55,"name":"name5"},{"age":66,"name":"name6"},{"age":77,"name":"name7"},{"age":77,"name":"name7"},{"age":88,"name":"name2"}]
选择字段过滤结果
["name1","name2","name3","name4","name5","name6","name7","name7","name2"]
["name1","name2","name3","name4","name5","name6","name7","name7","name2"]
去重复结果
条数限制结果
[{"age":11,"name":"name1"},{"age":22,"name":"name2"},{"age":33,"name":"name3"},{"age":44,"name":"name4"},{"age":55,"name":"name5"},{"age":66,"name":"name6"},{"age":77,"name":"name7"},{"age":88,"name":"name2"}]

[{"age":11,"name":"name1"},{"age":22,"name":"name2"},{"age":33,"name":"name3"},{"age":44,"name":"name4"}]统计结果
8扁平化结果

[["j","a","v","a","8"],["i","s"],["e","a","s","y"],["t","o"],["u","s","e"]]
[["j","v","8"],["is"],["e","sy"],["to"],["use"]]
规约结果
66
66
68
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,340评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,762评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,329评论 0 329
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,678评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,583评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,995评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,493评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,145评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,293评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,250评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,267评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,973评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,556评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,648评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,873评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,257评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,809评论 2 339