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

最后都可以使用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);}结果
【Java8新特性】- Stream流

文章插图
Stream的过滤与遍历stream的过滤是通过filter方法,通过实现匿名内部类new Predicate()的test方法,并且可以使用链式编程,持续过滤并且遍历,因为过滤不是终止运算 。然而forEach是实现匿名内部类new Consumer()的accept方法 。可以通过new的方式在通过idea来生成lambda表达式 。
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();stream.filter(student -> student.getName() != null).filter(student -> student.getScore() > 70).forEach(student -> System.out.println("name: " + student.getName() + " score: " + student.getScore()));}结果
【Java8新特性】- Stream流

文章插图
Stream的排序不仅Arrays以及数组能够实现排序甚至是重写排序规则,Stream流也是提供了相应的方法 。通过实现匿名内部类Comparator的compare方法 。
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();stream.sorted(new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {return o1.getScore() - o2.getScore();}}).forEach(new Consumer<Student>() {@Overridepublic void accept(Student student) {System.out.println("name: " + student.getName() + " score: " + student.getScore());}});/*lambda*/stream.sorted((o1, o2) -> o1.getScore() - o2.getScore()).forEach(student -> System.out.println("name: " + student.getName() + " score: " + student.getScore()));/*方法引入*/stream.sorted(Comparator.comparingInt(Student::getScore)).forEach(student -> System.out.println("name: " + student.getName() + " score: " + student.getScore()));}

推荐阅读