image.png

  1. List<User> list = Lists.newArrayList();
  2. User a = new User("a", "12", null, "121", LocalDateTime.now(), new Date());
  3. User b = new User("b", "12", "1223", "121", LocalDateTime.now(), new Date());
  4. User c = new User("c", "12", "1223", "121", LocalDateTime.now(), new Date());
  5. User d = new User("d", "12", "1223", "121", LocalDateTime.now(), new Date());
  6. User f = new User("a", "12", null, "121", LocalDateTime.now(), new Date());
  7. list.add(a);
  8. list.add(b);
  9. list.add(c);
  10. list.add(d);
  11. list.add(f);
  12. Map<String, String> collect = list.stream().collect(Collectors.toMap(User::getName, User::getPassword, (key1, key2) -> key2));

问题

如果是 继承 toMap的 话 value的值 不能说 null 不然会报错
image.png

正常的map 是可以的

需要对一个List中的对象进行唯一值属性去重,属性求和,对象假设为BillsNums,有id、nums、sums三个属性,其中id表示唯一值,需要nums与sums进行求和,并最后保持一份。
例如说:(“s1”, 1, 1),(“s1”,2,3),(“s2”,4,4), 求和并去重的话,就是(“s1”, 3, 4),(“s2”,4,4)

  1. ArrayList<OrgExMngYlwlCollectDay> arrayList = Lists.newArrayList();
  2. OrgExMngYlwlCollectDay orgExMngYlwlCollectDay = new OrgExMngYlwlCollectDay()
  3. .setInterceptOrgCode("1111")
  4. .setInterceptOrgName("测试")
  5. .setInterceptFailedSum(12)
  6. .setInterceptShouldSum(122);
  7. OrgExMngYlwlCollectDay orgExMngYlwlCollectDay1 = new OrgExMngYlwlCollectDay()
  8. .setInterceptOrgCode("1111")
  9. .setInterceptOrgName("测试")
  10. .setInterceptFailedSum(11)
  11. .setInterceptShouldSum(10);
  12. OrgExMngYlwlCollectDay orgExMngYlwlCollectDay2 = new OrgExMngYlwlCollectDay()
  13. .setInterceptOrgCode("111111")
  14. .setInterceptOrgName("测试")
  15. .setInterceptFailedSum(11)
  16. .setInterceptShouldSum(10);
  17. arrayList.add(orgExMngYlwlCollectDay1);
  18. arrayList.add(orgExMngYlwlCollectDay);
  19. arrayList.add(orgExMngYlwlCollectDay2);
  20. //核心代码 toMap KEY 为 code value 不变 如果两个重复 则 合并两个的 sum值
  21. List<OrgExMngYlwlCollectDay> collect = new ArrayList<>(arrayList.stream().collect(Collectors.toMap(OrgExMngYlwlCollectDay::getInterceptOrgCode, a -> a, (a1, a2) -> {
  22. if (a1.getInterceptOrgCode().equals(a2.getInterceptOrgCode())) {
  23. a1.setInterceptFailedSum(a1.getInterceptFailedSum() + a2.getInterceptFailedSum());
  24. a1.setInterceptShouldSum(a1.getInterceptShouldSum() + a2.getInterceptShouldSum());
  25. }
  26. return a1;
  27. })).values());

源码如下

  1. /**
  2. * Returns a {@code Collector} that accumulates elements into a
  3. * {@code Map} whose keys and values are the result of applying the provided
  4. * mapping functions to the input elements.
  5. *
  6. * <p>If the mapped
  7. * keys contains duplicates (according to {@link Object#equals(Object)}),
  8. * the value mapping function is applied to each equal element, and the
  9. * results are merged using the provided merging function.
  10. *
  11. * @apiNote
  12. * There are multiple ways to deal with collisions between multiple elements
  13. * mapping to the same key. The other forms of {@code toMap} simply use
  14. * a merge function that throws unconditionally, but you can easily write
  15. * more flexible merge policies. For example, if you have a stream
  16. * of {@code Person}, and you want to produce a "phone book" mapping name to
  17. * address, but it is possible that two persons have the same name, you can
  18. * do as follows to gracefully deals with these collisions, and produce a
  19. * {@code Map} mapping names to a concatenated list of addresses:
  20. * <pre>{@code
  21. * Map<String, String> phoneBook
  22. * people.stream().collect(toMap(Person::getName,
  23. * Person::getAddress,
  24. * (s, a) -> s + ", " + a));
  25. * }</pre>
  26. *
  27. * @implNote
  28. * The returned {@code Collector} is not concurrent. For parallel stream
  29. * pipelines, the {@code combiner} function operates by merging the keys
  30. * from one map into another, which can be an expensive operation. If it is
  31. * not required that results are merged into the {@code Map} in encounter
  32. * order, using {@link #toConcurrentMap(Function, Function, BinaryOperator)}
  33. * may offer better parallel performance.
  34. *
  35. * @param <T> the type of the input elements
  36. * @param <K> the output type of the key mapping function
  37. * @param <U> the output type of the value mapping function
  38. * @param keyMapper a mapping function to produce keys
  39. * @param valueMapper a mapping function to produce values
  40. * @param mergeFunction a merge function, used to resolve collisions between
  41. * values associated with the same key, as supplied
  42. * to {@link Map#merge(Object, Object, BiFunction)}
  43. * @return a {@code Collector} which collects elements into a {@code Map}
  44. * whose keys are the result of applying a key mapping function to the input
  45. * elements, and whose values are the result of applying a value mapping
  46. * function to all input elements equal to the key and combining them
  47. * using the merge function
  48. *
  49. * @see #toMap(Function, Function)
  50. * @see #toMap(Function, Function, BinaryOperator, Supplier)
  51. * @see #toConcurrentMap(Function, Function, BinaryOperator)
  52. */
  53. public static <T, K, U>
  54. Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
  55. Function<? super T, ? extends U> valueMapper,
  56. BinaryOperator<U> mergeFunction) {
  57. return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
  58. }

groupingBy

  1. //以userID分组
  2. Map<String, List<WorkScheduleExcel>> importWorkScheduleList = workExcelList.stream().collect(Collectors.groupingBy(WorkScheduleExcel::getUserId));