当前位置:   article > 正文

list.stream().forEach_list stream foreach

list stream foreach
        People people1 = new People(1, "Ligs", true);
        People people2 = new People(2, "Songzx", false);
        People people3 = new People(3, "Jinzg", true);
        People people4 = new People(4, "Liuzx", false);
        People people5 = new People(5, "Hedx", true);
        People people6 = new People(6, "Quansm", false);
        People people7 = new People(7, "Liangsz", true);
        People people8 = new People(8, "Chisz", true);
        ArrayList<People> list = new ArrayList<People>() {{
            add(people1);
            add(people2);
            add(people3);
            add(people4);
            add(people5);
            add(people6);
            add(people7);
            add(people8);
        }};
        System.out.println("======遍历=====");
        list.stream().forEach(item -> {
            System.out.println(item);
        });
        System.out.println("==============");
        System.out.println(list.stream().filter(People::getSex).collect(Collectors.toList()));
        //[People{id=1, name='Ligs', sex=true}, People{id=3, name='Jinzg', sex=true}, People{id=5, name='Hedx', sex=true}, People{id=7, name='Liangsz', sex=true}, People{id=8, name='Chisz', sex=true}]
        //过滤掉sex为false值的对象
        System.out.println(list.stream().map(People::getSex).collect(Collectors.toList()));
        //[true, false, true, false, true, false, true, true]
        //获取list中sex属性的集合
        list.stream().forEach(item ->{
            System.out.println(item);
        });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
======遍历=====
People{id=1, name='Ligs', sex=true}
People{id=2, name='Songzx', sex=false}
People{id=3, name='Jinzg', sex=true}
People{id=4, name='Liuzx', sex=false}
People{id=5, name='Hedx', sex=true}
People{id=6, name='Quansm', sex=false}
People{id=7, name='Liangsz', sex=true}
People{id=8, name='Chisz', sex=true}
==============
[People{id=1, name='Ligs', sex=true}, People{id=3, name='Jinzg', sex=true}, People{id=5, name='Hedx', sex=true}, People{id=7, name='Liangsz', sex=true}, People{id=8, name='Chisz', sex=true}]
[true, false, true, false, true, false, true, true]
People{id=1, name='Ligs', sex=true}
People{id=2, name='Songzx', sex=false}
People{id=3, name='Jinzg', sex=true}
People{id=4, name='Liuzx', sex=false}
People{id=5, name='Hedx', sex=true}
People{id=6, name='Quansm', sex=false}
People{id=7, name='Liangsz', sex=true}
People{id=8, name='Chisz', sex=true}

Process finished with exit code 0

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/569169
推荐阅读
相关标签
  

闽ICP备14008679号