集合的两种方式生成流

中间操作 主要有以下方法(此类型方法返回的都是Stream):map (mapToInt, flatMap 等)、 filter、 distinct、 sorted、 peek、 limit、 skip、 parallel、 sequential、 unordered
终止操作 主要有以下方法:forEach、 forEachOrdered、 toArray、 reduce、 collect、 min、 max、 count、 anyMatch、 allMatch、 noneMatch、 findFirst、 findAny、 iterator
创建实体类Student和数据

实体类Student有参数:id,name,age,address
1 2 3 4 5 6 7 8 9 10
| Student s1 = new Student(1L, "肖战", 15, "浙江"); Student s2 = new Student(2L, "王一博", 15, "湖北"); Student s3 = new Student(3L, "杨紫", 17, "北京"); Student s4 = new Student(4L, "李现", 17, "浙江");
List<Student> students = new ArrayList<>(); students.add(s1); students.add(s2); students.add(s3); students.add(s4);
|
filter(筛选)

刷选地址是浙江的学生集合List
1 2
| List<String> addresses = students.stream().filter(s -> "浙江".equals(s.getAddress())).collect(Collectors.toList());
|
map(转换)

结果:地址:浙江
地址:湖北
地址:北京
1 2
| List<String> addresses = students.stream().map(s ->"住址:"+s.getAddress()).collect(Collectors.toList()); addresses.forEach(a ->System.out.println(a));
|
distinct(去重)

注意:实体类的去重是无效的,必须要重写实体类的equals和hashCode()方法
1 2 3
| //简单字符串的去重 List<String> list = Arrays.asList("111","222","333","111","222"); list.stream().distinct().forEach(System.out::println);
|
1 2
| //实体类去重 students.stream().distinct().forEach(System.out::println);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| //实体类去重必须重写equals和hashCode()方法 @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(id, student.id) && Objects.equals(name, student.name) && Objects.equals(address, student.address); }
@Override public int hashCode() { return Objects.hash(id, name, age, address); }
|
sorted(排序)
1 2 3
| //简单字符串排序(默认排序) List<String> list = Arrays.asList("333","222","111"); list.stream().sorted().forEach(System.out::println);
|
1 2 3 4 5
| //按id倒序后,再按age倒序 students.stream() .sorted((stu1,stu2) ->Long.compare(stu2.getId(), stu1.getId())) .sorted((stu1,stu2) -> Integer.compare(stu2.getAge(),stu1.getAge())) .forEach(System.out::println);
|
limit(限制返回个数)
集合limit,返回前几个元素
结果:333
222
1 2
| List<String> list = Arrays.asList("333","222","111"); list.stream().limit(2).forEach(System.out::println);
|
skip(删除元素)
删除前几个元素
结果:111
1 2
| List<String> list = Arrays.asList("333","222","111"); list.stream().skip(2).forEach(System.out::println);
|
reduce(聚合)
1 2 3
| List<String> list = Arrays.asList("欢","迎","你"); String appendStr = list.stream().reduce("北京",(a,b) -> a+b); System.out.println(appendStr);
|
min和max(求最小/大值)
求集合中的元素最小/大值
1 2
| Student minS = students.stream().min((stu1,stu2) ->Integer.compare(stu1.getAge(),stu2.getAge())).get(); System.out.println(minS.toString());
|
anyMatch/allMatch/noneMatch(匹配)
anyMatch:Stream 中任意一个元素符合传入的 predicate,返回 true
allMatch:Stream 中全部元素符合传入的 predicate,返回 true
noneMatch:Stream 中没有一个元素符合传入的 predicate,返回 true
结果:有湖北人
所有学生都满15周岁
没有叫杨洋的同学
1 2 3 4 5 6 7 8 9 10 11 12
| Boolean anyMatch = students.stream().anyMatch(s ->"湖北".equals(s.getAddress())); if (anyMatch) { System.out.println("有湖北人"); } Boolean allMatch = students.stream().allMatch(s -> s.getAge()>=15); if (allMatch) { System.out.println("所有学生都满15周岁"); } Boolean noneMatch = students.stream().noneMatch(s -> "杨洋".equals(s.getName())); if (noneMatch) { System.out.println("没有叫杨洋的同学"); }
|