Stream流常见的中间方法

Stream流常见的终结方法
代码
学生类(代码一与代码二共涉及到的类)
java
package com.itheima.day28_Stream;
import java.util.Objects;
public class Student implements Comparable<Student> {
private String name;
private int age;
private double height;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
@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 && Double.compare(height, student.height) == 0 && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age, height);
}
public Student() {
}
public Student(String name, int age, double height) {
this.name = name;
this.age = age;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public int compareTo(Student o) {
return this.age - o.age;//年龄升序排序
}
}
代码一:掌握Stream流提供的常见中间方法
java
package com.itheima.day28_Stream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
//目标:掌握Stream流提供的常见中间方法
public class StreamTest3 {
public static void main(String[] args) {
List<Double> scores = new ArrayList<>();
Collections.addAll(scores,88.5,56.6,58.8,89.9,100.0,16.6);
// 需求1:找出成绩大于等于60分的数据,并升序后,再输出。
scores.stream().filter(s->s>=60).sorted().forEach(s->System.out.println(s));
List<Student>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())//使用lambda简写,年龄降序排列
.forEach(s->System.out.println(s));
System.out.println("===========================================");
//需求3:取出身高最高的前3名学生,并输出。
students.stream().sorted((o1,o2)->Double.compare(o2.getHeight(),o1.getHeight()))
.limit(3)
.forEach(System.out::println);
System.out.println("===========================================");
// 需求4:取出身高倒数的2名学生,并输出。
students.stream().sorted((o1,o2)->Double.compare(o2.getHeight(),o1.getHeight()))
.skip(students.size()-2)
.forEach(System.out::println);
System.out.println("===========================================");
// 需求5:找出身高超过168的学生叫什么名字,要求去除重复的名字,再输出。
students.stream().filter(s->s.getHeight()>168)
.map(Student::getName)//把原来的学生对象集合加工(映射)成为名字集合
.distinct()//去除重复的名字
.forEach(System.out::println);
System.out.println("===========================================");
// distinct去重复,自定义类型的对象(希望内容一样就认为重复,重写hashCode,equals)
students.stream().distinct()
.forEach(System.out::println);
System.out.println("===========================================");
//合并两个stream流
Stream<String> str1 = Stream.of("张三", "李四");
Stream<String> str2 = Stream.of("张3", "李4","王五");
Stream<String> Allst = Stream.concat(str1, str2);
Allst.forEach(System.out::println);
}
}
代码二:掌握Stream流提供的常见终结方法
java
package com.itheima.day28_Stream;
import java.util.*;
import java.util.stream.Collectors;
//目标:掌握Stream流提供的常见终结方法
public class StreamTest4 {
public static void main(String[] args) {
List<Student> 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 count = students.stream().filter(s -> s.getHeight() > 168).count();
System.out.println(count);
System.out.println("===================================");
// 需求2:请找出身高最高的学生对象,并输出。
Student stu1 = students.stream().max((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();
System.out.println(stu1);
System.out.println("===================================");
// 需求3:请找出身高最矮的学生对象,并输出。
Student stu2 = students.stream().min((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();
System.out.println(stu2);
System.out.println("===================================");
// 需求4:请找出身高超过170的学生对象,并放到一个新集合中去返回。
//流只能收集一次,因此"students.stream().filter(s -> s.getHeight() > 170)"不能单独作为新的stream流分别取做collect(Collectors.toList())与collect(Collectors.toSet())操作
List<Student> students1 = students.stream().filter(s -> s.getHeight() > 170).collect(Collectors.toList());
System.out.println(students1);
Set<Student> students2 = students.stream().filter(s -> s.getHeight() > 170).collect(Collectors.toSet());
System.out.println(students2);
// 需求5:请找出身高超过170的学生对象,并把学生对象的名字和身高,存入到一个Map集合返回。
System.out.println("===================================");
Map<String, Double> map = students.stream().filter(s -> s.getHeight() > 170)
.distinct()//map集合的元素不可重复,因此先去重
.collect(Collectors.toMap(a -> a.getName(), a -> a.getHeight()));
System.out.println(map);
System.out.println("===================================");
//存入到数组
//写法1
Object[] array1 = students.stream().filter(s -> s.getHeight() > 170)
.toArray();
System.out.println(Arrays.toString(array1));
System.out.println("======================================");
//写法2
Student[] array2 = students.stream().filter(s -> s.getHeight() > 170)
.toArray(len -> new Student[len]);
System.out.println(Arrays.toString(array2));
}
}
