【Java8新特性】- Stream流( 二 )

来声明key和value 。如下代码,可以这么理解,stream.collect(Collectors.toMap(key, value)),key和value都是通过new Function<T, K>,对于key:T指的是Steam流的类型(既Student),而K代表的是map中的key值,因此这里是String类型的,在apply方法中去返回key值,通过student的名字来最为key,因此这里返回student.getName() 。而第二个作为map的value,整体操作也是跟key差不多,主要还是需要注意的是,value存的是student,因此需要使用Student类型 。
Map<String, Student> map = stream.collect(Collectors.toMap(new Function<Student, String>() {@Overridepublic String apply(Student student) {return student.getName();}}, new Function<Student, Student>() {@Overridepublic Student apply(Student student) {return student;}}));最后都可以使用lambda表达式来
public static void main(String[] args) {// Stream将list转换为MapArrayList<Student> students = new ArrayList<>();students.add(new Student("lyd", 99));students.add(new Student("lkj", 55));students.add(new Student("llm", 67));students.add(new Student("lss", 87));Stream<Student> stream = students.stream();Map<String, Student> map = stream.collect(Collectors.toMap(student -> student.getName(), student -> student));map.forEach((key, value) -> System.out.println("key: " + key + " -> value: " + value));}结果

【Java8新特性】- Stream流

文章插图
Stream使用Reduce求和通过使用stream的reduce方法,在里面去new BinaryOperator,代码如下
public static void main(String[] args) {// Stream使用Reduce求和ArrayList<Student> students = new ArrayList<>();students.add(new Student("lyd", 99));students.add(new Student("lkj", 55));students.add(new Student("llm", 67));students.add(new Student("lss", 87));Stream<Student> stream = students.stream();Optional<Student> sum = stream.reduce((student, student2) -> {Student sum1 = new Student("sum", student.getScore() + student2.getScore());return sum1;});System.out.println(sum.get().getName() + " : " + sum.get().getScore());}结果
【Java8新特性】- Stream流

文章插图
Stream使用Max和Min实际上就是通过匿名内部类new Comparator()实现public int compare(Student o1, Student o2)比较方法 。
public static void main(String[] args) {// StreamMax和MinArrayList<Student> students = new ArrayList<>();students.add(new Student("lyd", 99));students.add(new Student("lkj", 55));students.add(new Student("llm", 67));students.add(new Student("lss", 87));Stream<Student> stream = students.stream();Optional<Student> max = stream.max((o1, o2) -> o1.getScore() - o2.getScore());System.out.println(max.get().getScore());Stream<Student> stream2 = students.stream();Optional<Student> min = stream2.min((o1, o2) -> o1.getScore() - o2.getScore());// 可以使用方法引入更加简便// Optional<Student> min = stream2.min(Comparator.comparingInt(Student::getScore));System.out.println(min.get().getScore());}结果
【Java8新特性】- Stream流

文章插图
Stream中Match匹配
  • anyMatch表示,判断的条件里,任意一个元素成功,返回true
  • allMatch表示,判断条件里的元素,所有的都是,返回true
  • noneMatch跟allMatch相反,判断条件里的元素,所有的都不是,返回true
public static void main(String[] args) {ArrayList<Student> students = new ArrayList<>();students.add(new Student("lyd", 99));students.add(new Student("lkj", 55));students.add(new Student("llm", 67));students.add(new Student("lss", 87));Stream<Student> stream = students.stream();boolean allMatch = stream.allMatch(student -> student.getScore() > 60);System.out.println("allMatch: " + allMatch);Stream<Student> stream2 = students.stream();boolean anyMatch = stream2.anyMatch(student -> student.getScore() > 60);System.out.println("anyMatch: " + anyMatch);Stream<Student> stream3 = students.stream();boolean noneMatch = stream3.noneMatch(student -> student.getScore() > 60);System.out.println("noneMatch: " + noneMatch);}

经验总结扩展阅读