List<User> list = Lists.newArrayList();
User a = new User("a", "12", null, "121", LocalDateTime.now(), new Date());
User b = new User("b", "12", "1223", "121", LocalDateTime.now(), new Date());
User c = new User("c", "12", "1223", "121", LocalDateTime.now(), new Date());
User d = new User("d", "12", "1223", "121", LocalDateTime.now(), new Date());
User f = new User("a", "12", null, "121", LocalDateTime.now(), new Date());
list.add(a);
list.add(b);
list.add(c);
list.add(d);
list.add(f);
Map<String, String> collect = list.stream().collect(Collectors.toMap(User::getName, User::getPassword, (key1, key2) -> key2));
问题
如果是 继承 toMap的 话 value的值 不能说 null 不然会报错
正常的map 是可以的
需要对一个List中的对象进行唯一值属性去重,属性求和,对象假设为BillsNums,有id、nums、sums三个属性,其中id表示唯一值,需要nums与sums进行求和,并最后保持一份。
例如说:(“s1”, 1, 1),(“s1”,2,3),(“s2”,4,4), 求和并去重的话,就是(“s1”, 3, 4),(“s2”,4,4)
ArrayList<OrgExMngYlwlCollectDay> arrayList = Lists.newArrayList();
OrgExMngYlwlCollectDay orgExMngYlwlCollectDay = new OrgExMngYlwlCollectDay()
.setInterceptOrgCode("1111")
.setInterceptOrgName("测试")
.setInterceptFailedSum(12)
.setInterceptShouldSum(122);
OrgExMngYlwlCollectDay orgExMngYlwlCollectDay1 = new OrgExMngYlwlCollectDay()
.setInterceptOrgCode("1111")
.setInterceptOrgName("测试")
.setInterceptFailedSum(11)
.setInterceptShouldSum(10);
OrgExMngYlwlCollectDay orgExMngYlwlCollectDay2 = new OrgExMngYlwlCollectDay()
.setInterceptOrgCode("111111")
.setInterceptOrgName("测试")
.setInterceptFailedSum(11)
.setInterceptShouldSum(10);
arrayList.add(orgExMngYlwlCollectDay1);
arrayList.add(orgExMngYlwlCollectDay);
arrayList.add(orgExMngYlwlCollectDay2);
//核心代码 toMap KEY 为 code value 不变 如果两个重复 则 合并两个的 sum值
List<OrgExMngYlwlCollectDay> collect = new ArrayList<>(arrayList.stream().collect(Collectors.toMap(OrgExMngYlwlCollectDay::getInterceptOrgCode, a -> a, (a1, a2) -> {
if (a1.getInterceptOrgCode().equals(a2.getInterceptOrgCode())) {
a1.setInterceptFailedSum(a1.getInterceptFailedSum() + a2.getInterceptFailedSum());
a1.setInterceptShouldSum(a1.getInterceptShouldSum() + a2.getInterceptShouldSum());
}
return a1;
})).values());
源码如下
/**
* Returns a {@code Collector} that accumulates elements into a
* {@code Map} whose keys and values are the result of applying the provided
* mapping functions to the input elements.
*
* <p>If the mapped
* keys contains duplicates (according to {@link Object#equals(Object)}),
* the value mapping function is applied to each equal element, and the
* results are merged using the provided merging function.
*
* @apiNote
* There are multiple ways to deal with collisions between multiple elements
* mapping to the same key. The other forms of {@code toMap} simply use
* a merge function that throws unconditionally, but you can easily write
* more flexible merge policies. For example, if you have a stream
* of {@code Person}, and you want to produce a "phone book" mapping name to
* address, but it is possible that two persons have the same name, you can
* do as follows to gracefully deals with these collisions, and produce a
* {@code Map} mapping names to a concatenated list of addresses:
* <pre>{@code
* Map<String, String> phoneBook
* people.stream().collect(toMap(Person::getName,
* Person::getAddress,
* (s, a) -> s + ", " + a));
* }</pre>
*
* @implNote
* The returned {@code Collector} is not concurrent. For parallel stream
* pipelines, the {@code combiner} function operates by merging the keys
* from one map into another, which can be an expensive operation. If it is
* not required that results are merged into the {@code Map} in encounter
* order, using {@link #toConcurrentMap(Function, Function, BinaryOperator)}
* may offer better parallel performance.
*
* @param <T> the type of the input elements
* @param <K> the output type of the key mapping function
* @param <U> the output type of the value mapping function
* @param keyMapper a mapping function to produce keys
* @param valueMapper a mapping function to produce values
* @param mergeFunction a merge function, used to resolve collisions between
* values associated with the same key, as supplied
* to {@link Map#merge(Object, Object, BiFunction)}
* @return a {@code Collector} which collects elements into a {@code Map}
* whose keys are the result of applying a key mapping function to the input
* elements, and whose values are the result of applying a value mapping
* function to all input elements equal to the key and combining them
* using the merge function
*
* @see #toMap(Function, Function)
* @see #toMap(Function, Function, BinaryOperator, Supplier)
* @see #toConcurrentMap(Function, Function, BinaryOperator)
*/
public static <T, K, U>
Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,
BinaryOperator<U> mergeFunction) {
return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
}
groupingBy
//以userID分组
Map<String, List<WorkScheduleExcel>> importWorkScheduleList = workExcelList.stream().collect(Collectors.groupingBy(WorkScheduleExcel::getUserId));