Student s1 = new Student("aa", 10, 1);
Student s2 = new Student("bb", 20, 2);
Student s3 = new Student("cc", 10, 3);
List<Student> list = Arrays.asList(s1, s2, s3);
List<Integer> ageList = list.stream().map(Student::getAge).collect(Collectors.toList());
Set<Integer> ageSet = list.stream().map(Student::getAge).collect(Collectors.toSet());
Map<String, Integer> studentMap = list.stream().collect(Collectors.toMap(Student::getName, Student::getAge));
String joinName = list.stream().map(Student::getName).collect(Collectors.joining(",", "(", ")"));
Long count = list.stream().collect(Collectors.counting());
Integer maxAge = list.stream().map(Student::getAge).collect(Collectors.maxBy(Integer::compare)).get();
Integer maxAge2 = list.stream().max(Comparator.comparing(Student::getAge)).get().getAge();
Integer sumAge = list.stream().collect(Collectors.summingInt(Student::getAge));
Double averageAge = list.stream().collect(Collectors.averagingDouble(Student::getAge));
DoubleSummaryStatistics statistics = list.stream().collect(Collectors.summarizingDouble(Student::getAge));
System.out.println("count:" + statistics.getCount() + ",max:" + statistics.getMax() + ",sum:" + statistics.getSum() + ",average:" + statistics.getAverage());
//分组
Map<Integer, List<Student>> ageMap = list.stream().collect(Collectors.groupingBy(Student::getAge));
//多重分组,先根据类型分再根据年龄分
Map<Integer, Map<Integer, List<Student>>> typeAgeMap = list.stream().collect(Collectors.groupingBy(Student::getType, Collectors.groupingBy(Student::getAge)));
//分区
//分成两部分,一部分大于10岁,一部分小于等于10岁
Map<Boolean, List<Student>> partMap = list.stream().collect(Collectors.partitioningBy(v -> v.getAge() > 10));
//规约
Integer allAge = list.stream().map(Student::getAge).collect(Collectors.reducing(Integer::sum)).get();
过滤List中重复属性字段,
private static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
Map排序,
collect.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(e -> {
collect.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).forEach
stream分组求和,
Map<String, Double> sum = new HashMap<>();
sum = appletHomeStockWxSbs.stream().collect(Collectors.groupingBy(AppletHomeStockWxSbVo::getArea, Collectors.summingDouble(AppletHomeStockWxSbVo::getInventoryArea)));
分组求最大,
Map<String, AppletBankLoanRateWxSb> collect = tbList.parallelStream().filter(e -> e.getPublicTime() != null).collect(Collectors.groupingBy(AppletBankLoanRateWxSb::getBankName,Collectors.collectingAndThen(Collectors.reducing((c1, c2) -> c1.getPublicTime().compareTo(c2.getPublicTime()) > 0 ? c1 : c2), Optional::get)));
分页,
int page = dto.getPageIndex();
int limit = dto.getPageSize();
int startRow = page * limit - limit;
int totalPage = 0;
if (enterpriseWxSbVoList.size() % limit > 0) {
totalPage = (enterpriseWxSbVoList.size() / limit) + 1;
} else {
totalPage = enterpriseWxSbVoList.size() / limit;
}
List<BankLoanRateWxSbVo> rList = enterpriseWxSbVoList.stream().skip(startRow).limit(limit).collect(Collectors.toList());