import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
List names = new ArrayList<>();
Collections.addAll(names,"张三丰","张无忌","周芷若","赵敏","张强");
System.out.println(names);
List list = names.stream().filter(s -> s.startsWith("张"))
.filter(a -> a.length()==3).collect(Collectors.toList());
System.out.println(list);
}
}
Collection提供的如下方法 | 说明 |
---|---|
default Stream |
获取当前集合对象的Stream流 |
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
public class Test {
public static void main(String[] args) {
List 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));
List 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,183.5);
Student s5 = new Student("赵六",35,167.0);
Collections.addAll(students,s1,s2,s3,s4,s5);
//需求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(s -> System.out.println(s));
//需求4:取出身高倒数的2名学生,并输出
students.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(),o1.getHeight()))
.skip(students.size() - 2).forEach(s -> System.out.println(s));
//需求5:找出身高超过168的学生叫说明名字,要求去除重复的名字,再输出
students.stream().filter(s -> s.getHeight() > 168).map(Student::getName)
.distinct().forEach(s -> System.out.println(s));
// distinct去重复,自定义类型的对象(希望内容一样就认为重复,重写hashCode,equals)
students.stream().filter(s -> s.getHeight() >168)
.distinct().forEach(System.out::println);
// 合并流
Stream str1 = Stream.of("张三","李四");
Stream str2 = Stream.of("王五","赵六");
Stream allStr = Stream.concat(str1,str2);
allStr.forEach(s -> System.out.println());
}
}
import java.util.*;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
List 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,183.5);
Student s5 = new Student("赵六",35,167.0);
Collections.addAll(students,s1,s2,s3,s4,s5);
// 需求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的学生对象,并放到一个新的集合中去
//流只能收集一次
List students1 = students.stream().filter(a ->a.getHeight() > 170).collect(Collectors.toList());
System.out.println(students1);
Set students2 = students.stream().filter(a -> a.getHeight() > 170).collect(Collectors.toSet());
System.out.println(students2);
// 需求5:找出身高超过170的学生对象,并把学生对象的名字和身高,存入到一个Map集合返回
Map map =
students.stream().filter(a -> a.getHeight() > 170)
.distinct().collect(Collectors.toMap(a -> a.getName(),a -> a.getHeight()));
System.out.println(map);
Student[] arr = students.stream().filter(a -> a.getHeight() > 170).toArray(len -> new Student[len]);
System.out.println(Arrays.toString(arr));
}
}