记录一下用到的一些java8的lambda表达式语法
1 list集合根据某个字段分组后求多个字段的和 1 2 3 4 5 6 7 8 9 10 11 12 13 List<SafeSystemVO> list = new ArrayList<>(16 ); list.stream().collect(Collectors.groupingBy(SafeSystemVO::getDriver_id)).values().stream().map(d -> { SafeSystemVO vo = d.get(0 ); vo.setAg_total(d.stream().map(s -> BigDecimal.valueOf(s.getAg_total())).reduce(BigDecimal.ZERO, BigDecimal::add).doubleValue()); vo.setScore(d.stream().map(s -> BigDecimal.valueOf(s.getScore())).reduce(BigDecimal.ZERO, BigDecimal::add).doubleValue()); vo.setLkj_score(d.stream().map(s -> BigDecimal.valueOf(s.getLkj_score())).reduce(BigDecimal.ZERO, BigDecimal::add).doubleValue()); vo.setTotalScore(d.stream().map(s -> BigDecimal.valueOf(s.getTotalScore())).reduce(BigDecimal.ZERO, BigDecimal::add).doubleValue()); return vo; }).collect(Collectors.toList());
2. list 根据某个字段分组后求单个字段的平均值,并按照分组字段排序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 List<SafeSystemVO> list = new ArrayList<>(16 ); Map<String, Double> monthAvg = list1.stream().collect(Collectors.groupingBy(SafeSystemVO::getMonth, Collectors.averagingDouble(SafeSystemVO::getTotalScore))); monthAvg.entrySet().stream().sorted((o1, o2) -> { try { Date d1 = DateUtils.convertStringToDate(o1.getKey(), DateUtils.FM2); Date d2 = DateUtils.convertStringToDate(o2.getKey(), DateUtils.FM2); assert d1 != null ; return d1.compareTo(d2); } catch (Exception e) { e.printStackTrace(); } return 0 ; }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldVal, newVal) -> oldVal, LinkedHashMap::new ));
3. list 根据字段分组求和后取 前/后10名 1 2 3 4 5 6 7 8 Map<String, Double> driverScores = list3.stream() .collect(Collectors.groupingBy(SafeSystemVO::getDriver_id, Collectors.averagingDouble(SafeSystemVO::getTotalScore))); List<Map.Entry<String, Double>> driverScoresTop10 = driverScores.entrySet().stream().sorted((p1, p2) -> p1.getValue().compareTo(p2.getValue())).limit(10 ).collect(Collectors.toList());
4. 其他 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 public static void main (String[] args) { Person p1 = new Person(1 ,"麻子" , 31 ); Person p2 = new Person(2 ,"李四" , 20 ); Person p3 = new Person(3 ,"王五" , 26 ); Person p4 = new Person(3 ,"王五" , 26 ); List<Person> personList = new ArrayList<Person>(); personList.add(p1); personList.add(p2); personList.add(p3); personList.add(p4); personList.forEach(p -> System.out.println(p.getAge())); personList.sort((o1, o2) -> o1.getAge().compareTo(o2.getAge())); personList.sort((o1, o2) -> o2.getAge().compareTo(o1.getAge())); personList.sort(Comparator.comparing(Person::getAge)); personList.sort(Comparator.comparing(Person::getAge).reversed()); personList.sort(Comparator.comparing(User::getId).thenComparing(Person::getAge)); System.out.println("========================================" ); Person maxAgePerson = personList.stream().max(Comparator.comparing(Person::getAge)).get(); System.out.println(maxAgePerson.getAge()); System.out.println("========================================" ); Person minAgePerson = personList.stream().min(Comparator.comparing(Person::getAge)).get(); System.out.println(minAgePerson.getAge()); List<Person> personList1 = personList.stream().filter(person -> person.getAge() == 20 ).collect(Collectors.toList()); long count = personList.stream().filter(person -> person.getAge() == 20 ).count(); List<Person> personList2 = personList.stream().filter(t -> t.getAge().equals(20 )).collect(Collectors.toList()); double asDouble = personList.stream().mapToInt(person -> person.getAge()).average().getAsDouble(); int sum = personList.stream().mapToInt(person -> person.getAge()).sum(); BigDecimal totalAge = personList.stream().map(User::getAge).reduce(BigDecimal.ZERO, BigDecimal::add); (其中,若bigDecimal对象为null ,可filter()过滤掉空指针.) List<Person> personList3 = personList.stream().distinct().collect(Collectors.toList()); Map<Long, Person> personMap = personList.stream().collect(Collectors.toMap(User::getId, t -> t,(k1,k2)->k1)); } }