广州小型网站建设公司,做ppt的图片网站,湘潭网站建设方案案例,中核工建设集团OA网站一、JDK8新特性#xff08;Stream流#xff09;
接下来学习一个全新的知识#xff0c;叫做Stream流#xff08;也叫Stream API#xff09;。它是从JDK8以后才有的一个新特性#xff0c;是专业用于对集合或者数组进行便捷操作的。有多方便呢#xff1f;我们用一个案例体…一、JDK8新特性Stream流
接下来学习一个全新的知识叫做Stream流也叫Stream API。它是从JDK8以后才有的一个新特性是专业用于对集合或者数组进行便捷操作的。有多方便呢我们用一个案例体验一下然后再详细学习。
1.1 Stream流体验
案例需求有一个List集合元素有张三丰,张无忌,周芷若,赵敏,张强找出姓张且是3个字的名字存入到一个新集合中去。
ListString names new ArrayList();
Collections.addAll(names, 张三丰,张无忌,周芷若,赵敏,张强);
System.out.println(names);用传统方式来做代码是这样的
// 找出姓张且是3个字的名字存入到一个新集合中去。
ListString list new ArrayList();
for (String name : names) {if(name.startsWith(张) name.length() 3){list.add(name);}
}
System.out.println(list);用Stream流来做代码是这样的ps: 是不是想流水线一样一句话就写完了
ListString list2 names.stream().filter(s - s.startsWith(张)).filter(a - a.length()3).collect(Collectors.toList());
System.out.println(list2);先不用知道这里面每一句话是什么意思具体每一句话的含义待会再一步步学习。现在只是体验一下。
学习Stream流我们接下来会按照下面的步骤来学习。
1.2 Stream流的创建
好接下来我们正式来学习Stream流。先来学习如何创建Stream流、或者叫获取Stream流。
主要掌握下面四点1、如何获取List集合的Stream流2、如何获取Set集合的Stream流3、如何获取Map集合的Stream流4、如何获取数组的Stream流直接上代码演示
/*** 目标掌握Stream流的创建。*/
public class StreamTest2 {public static void main(String[] args) {// 1、如何获取List集合的Stream流ListString names new ArrayList();Collections.addAll(names, 张三丰,张无忌,周芷若,赵敏,张强);StreamString stream names.stream();// 2、如何获取Set集合的Stream流SetString set new HashSet();Collections.addAll(set, 刘德华,张曼玉,蜘蛛精,马德,德玛西亚);StreamString stream1 set.stream();stream1.filter(s - s.contains(德)).forEach(s - System.out.println(s));// 3、如何获取Map集合的Stream流MapString, Double map new HashMap();map.put(古力娜扎, 172.3);map.put(迪丽热巴, 168.3);map.put(马尔扎哈, 166.3);map.put(卡尔扎巴, 168.3);SetString keys map.keySet();StreamString ks keys.stream();CollectionDouble values map.values();StreamDouble vs values.stream();SetMap.EntryString, Double entries map.entrySet();StreamMap.EntryString, Double kvs entries.stream();kvs.filter(e - e.getKey().contains(巴)).forEach(e - System.out.println(e.getKey() -- e.getValue()));// 4、如何获取数组的Stream流String[] names2 {张翠山, 东方不败, 唐大山, 独孤求败};StreamString s1 Arrays.stream(names2);StreamString s2 Stream.of(names2);}
}
1.3 Stream流中间方法
中间方法指的是调用完方法之后其结果是一个新的Stream流于是可以继续调用方法这样一来就可以支持链式编程或者叫流式编程。
话不多说直接上代码演示
/*** 目标掌握Stream流提供的常见中间方法。*/
public class StreamTest3 {public static void main(String[] args) {ListDouble scores new ArrayList();Collections.addAll(scores, 88.5, 100.0, 60.0, 99.0, 9.5, 99.6, 25.0);// 需求1找出成绩大于等于60分的数据并升序后再输出。scores.stream().filter(s - s 60).sorted().forEach(s - System.out.println(s));ListStudent students new ArrayList();Student s1 new Student(蜘蛛精, 26, 172.5);Student s2 new Student(蜘蛛精, 26, 172.5);Student s3 new Student(紫霞, 23, 167.6);Student s4 new Student(白晶晶, 25, 169.0);Student s5 new Student(牛魔王, 35, 183.3);Student s6 new Student(牛夫人, 34, 168.5);Collections.addAll(students, s1, s2, s3, s4, s5, s6);// 需求2找出年龄大于等于23,且年龄小于等于30岁的学生并按照年龄降序输出.students.stream().filter(s - s.getAge() 23 s.getAge() 30).sorted((o1, o2) - o2.getAge() - o1.getAge()).forEach(s - System.out.println(s));// 需求3取出身高最高的前3名学生并输出。students.stream().sorted((o1, o2) - Double.compare(o2.getHeight(), o1.getHeight())).limit(3).forEach(System.out::println);System.out.println(-----------------------------------------------);// 需求4取出身高倒数的2名学生并输出。 s1 s2 s3 s4 s5 s6students.stream().sorted((o1, o2) - Double.compare(o2.getHeight(), o1.getHeight())).skip(students.size() - 2).forEach(System.out::println);// 需求5找出身高超过168的学生叫什么名字要求去除重复的名字再输出。students.stream().filter(s - s.getHeight() 168).map(Student::getName).distinct().forEach(System.out::println);// distinct去重复自定义类型的对象希望内容一样就认为重复重写hashCode,equalsstudents.stream().filter(s - s.getHeight() 168).distinct().forEach(System.out::println);StreamString st1 Stream.of(张三, 李四);StreamString st2 Stream.of(张三2, 李四2, 王五);StreamString allSt Stream.concat(st1, st2);allSt.forEach(System.out::println);}
}1.4 Stream流终结方法
最后我们再学习Stream流的终结方法。这些方法的特点是调用完方法之后其结果就不再是Stream流了所以不支持链式编程。 话不多说直接上代码
/*** 目标Stream流的终结方法*/
public class StreamTest4 {public static void main(String[] args) {ListStudent students new ArrayList();Student s1 new Student(蜘蛛精, 26, 172.5);Student s2 new Student(蜘蛛精, 26, 172.5);Student s3 new Student(紫霞, 23, 167.6);Student s4 new Student(白晶晶, 25, 169.0);Student s5 new Student(牛魔王, 35, 183.3);Student s6 new Student(牛夫人, 34, 168.5);Collections.addAll(students, s1, s2, s3, s4, s5, s6);// 需求1请计算出身高超过168的学生有几人。long size students.stream().filter(s - s.getHeight() 168).count();System.out.println(size);// 需求2请找出身高最高的学生对象并输出。Student s students.stream().max((o1, o2) - Double.compare(o1.getHeight(), o2.getHeight())).get();System.out.println(s);// 需求3请找出身高最矮的学生对象并输出。Student ss students.stream().min((o1, o2) - Double.compare(o1.getHeight(), o2.getHeight())).get();System.out.println(ss);// 需求4请找出身高超过170的学生对象并放到一个新集合中去返回。// 流只能收集一次。ListStudent students1 students.stream().filter(a - a.getHeight() 170).collect(Collectors.toList());System.out.println(students1);SetStudent students2 students.stream().filter(a - a.getHeight() 170).collect(Collectors.toSet());System.out.println(students2);// 需求5请找出身高超过170的学生对象并把学生对象的名字和身高存入到一个Map集合返回。MapString, Double map students.stream().filter(a - a.getHeight() 170).distinct().collect(Collectors.toMap(a - a.getName(), a - a.getHeight()));//把什么作为键把什么作为值System.out.println(map);// Object[] arr students.stream().filter(a - a.getHeight() 170).toArray();Student[] arr students.stream().filter(a - a.getHeight() 170).toArray(len - new Student[len]);System.out.println(Arrays.toString(arr));}
}