java stream分组排序统计求和
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);
}