java stream多條件分組
其中Student是學生,將學生依次以grade(年級) -> class(班級) -> teacher(任課老師) 分組
Map<String,Map<String, Map<String, List<Student>>>> result = students.stream().collect(
Collectors.groupingBy(Student::getGrade,
Collectors.groupingBy(Student::getClass,
Collectors.groupingBy(Student::getTeacher)))
);
多條件去重
students.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(o -> o.getAge() + ";" + o.getName()))), ArrayList::new)).forEach(s -> println(s));
filter過濾
students.stream().filter(s -> s.getAge() == 10).forEach(s -> println(s));
sorted排序
students.stream().sorted(Comparator.comparing(s-> s.getAge())).forEach(s -> println(s));
1.自然序排序
students.stream().sorted();
2.自然序逆序元素,使用Comparator 提供的reverseOrder() 方法
students.stream().sorted(Comparator.reverseOrder());
3.按照年齡倒序排序
students.stream().sorted(Comparator.comparing(Student::getAge).reversed());
4.不借助stream排序
分數正序排序
chineseScores.sort(Comparator.comparing(Integer::intValue));
分數倒序排序
chineseScores.sort(Comparator.comparing(Integer::intValue).reversed());
年齡正序排序
students.sort(Comparator.comparing(Student::getAge));
年齡倒序排序
students.sort(Comparator.comparing(Student::getAge).reversed());
limit方法限制最多返回多少元素
students.stream().limit(2).forEach(s -> println(s));
不要前多n個元素,n大於滿足條件的元素個數就返回空的流
students.stream().skip(2).forEach(s -> println(s));
最大值 最小值
Optional<User> min = students.stream().min(Comparator.comparing(Student::getAge));
println(min);
Optional<User> max = students.stream().max(Comparator.comparing(Student::getAge));
println(max);
轉單數組遍歷
students.stream().map(Student::getName).forEach(name -> println(name));
students.stream().mapToInt(Student::getAge).forEach(age -> println(age));
students.stream().mapToDouble(Student::getScoreOfChinese).forEach(scoreOfChinese -> println(scoreOfChinese));
students.stream().mapToLong(Student::getAge).forEach(getAge -> println(getAge));
轉單數組求和
students.stream().mapToDouble(Student::getScoreOfChinese).sum();
查找匹配指定數據是否存在
allMatch方法與anyMatch差不多,表示所有的元素都滿足才返回true。noneMatch方法表示沒有元素滿足
boolean anyMatch = students.stream().anyMatch(s -> s.getAge() == 100);
boolean allMatch = students.stream().allMatch(s -> s.getName() == 'hello word');
boolean noneMatch = students.stream().noneMatch(s -> s.getStudentId() == '10010');
簡化操作 最大值,最小值,求和
Optional<Integer> sum = list.stream().map(User::getAge).reduce(Integer::sum);
Optional<Integer> max = list.stream().map(User::getAge).reduce(Integer::max);
Optional<Integer> min = list.stream().map(User::getAge).reduce(Integer::min);
println(sum);
println(max);
println(min);
統計
IntSummaryStatistics statistics = students.stream().collect(Collectors.summarizingInt(User::getAge));
double average = statistics.getAverage();
long count = statistics.getCount();
int max = statistics.getMax();
int min = statistics.getMin();
long sum = statistics.getSum();
轉set
Set<User> collect = students.stream().collect(Collectors.toSet());
Iterator<User> iterator = collect.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next().getUserId());
}
轉map
Map<String, User> collect = students.stream().collect(Collectors.toMap(Student::getName, s -> s));
for (String name : collect.keySet()) {
//得到每個key對應多個value的值
Student s = collect.get(name);
println(s);
}
