Stream流中间方法
public class Demo01 {
public static void main(String[] args) {
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);
List<Student> list = new ArrayList<>();
Collections.addAll(list,s1,s2,s3,s4,s5,s6);
//filter 过滤
//sort 排序
list.stream().
filter(student -> student.getHeight() > 170).
sorted(Comparator.comparing(Student::getAge)).
forEach(student -> System.out.println(student));
System.out.println("-----------------------------------");
list.stream().filter(student -> student.getAge() > 30)
.sorted((o1, o2) -> o2.getAge() - o1.getAge())
.forEach(student -> System.out.println(student));
System.out.println("-------------------------");
//limit 获取前几个元素
list.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(),o1.getHeight()))
.limit(3)
.forEach(student -> System.out.println(student));
System.out.println("----------------------------------------------");
//skip 跳过前几个元素
list.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(),o1.getHeight()))
.skip(list.size()-2)
.forEach(student -> System.out.println(student));
System.out.println("----------------------------------------------");
//distinct 去除重复元素
list.stream().filter(student -> student.getHeight()>170)
.map(student -> student.getHeight())
.distinct()
.forEach(student -> System.out.println(student));
System.out.println("----------------------------------------------");
//concat 连接两个流
Stream<String> stream1 = Stream.of("阿大", "阿二");
Stream<String> stream2 = Stream.of("阿三", "阿四");
Stream.concat(stream1,stream2).forEach(student -> System.out.println(student));
}
}
Stream流终结方法
public class Demo02 {
public static void main(String[] args) {
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);
List<Student> list = new ArrayList<>();
Collections.addAll(list,s1,s2,s3,s4,s5,s6);
//count 统计元素个数
System.out.println(list.stream().filter(student -> student.getHeight() < 170).count());
//max 获取最大值
System.out.println(list.stream().max((o1, o2) -> Double.compare(o1.getHeight(),o2.getHeight())).get());
//min 获取最小值
System.out.println(list.stream().min(Comparator.comparingDouble(Student::getHeight)).get());
System.out.println("__________________________________________");
//collect 将Stream流中的元素收集到一个集合中
System.out.println(list.stream().filter(student -> student.getAge() > 30).collect(Collectors.toList()));
System.out.println(list.stream().filter(student -> student.getAge() < 30).collect(Collectors.toSet()));
System.out.println("----------------------------------------------");
list.stream().filter(student -> student.getAge() > 30)
.collect(Collectors.toMap(student -> student.getName(), student -> student.getHeight()))
.forEach((s, aDouble) -> System.out.println("key="+s+",value="+aDouble));
}
}
标签:Student,Stream,list,System,student,println,out
From: https://blog.51cto.com/u_16245664/7593494