@Test
public void listSample() {
List<Temp> list = Arrays.asList(
Temp.builder().group("A").uuid(UUID.randomUUID().toString()).build(),
Temp.builder().group("A").uuid(UUID.randomUUID().toString()).build(),
Temp.builder().group("B").uuid(UUID.randomUUID().toString()).build(),
Temp.builder().group("C").uuid(UUID.randomUUID().toString()).build()
);
list.stream()
.collect(Collectors.groupingBy(Temp::getGroup))
.forEach((key, value) -> {
log.info("key :{}, value : {}", key, value.size());
});
}
key :A, value : 2
key :B, value : 1
key :C, value : 1
Caused by: java.lang.NullPointerException: element cannot be mapped to a null key
at java.util.Objects.requireNonNull(Objects.java:228)
at java.util.stream.Collectors.lambda$groupingBy$45(Collectors.java:907)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
@Test
public void listSample() {
List<Temp> list = Arrays.asList(
Temp.builder().group("A").uuid(UUID.randomUUID().toString()).build(),
Temp.builder().group("A").uuid(UUID.randomUUID().toString()).build(),
Temp.builder().group("B").uuid(UUID.randomUUID().toString()).build(),
Temp.builder().group("C").uuid(UUID.randomUUID().toString()).build(),
Temp.builder().group(null).uuid(UUID.randomUUID().toString()).build(),
Temp.builder().group(null).uuid(UUID.randomUUID().toString()).build()
);
list.stream()
.collect(
Collectors.toMap(
Temp::getGroup,
x -> {
List<Temp> subList = new ArrayList<>();
subList.add(x);
return subList;
},
(left, right) -> {
left.addAll(right);
return left;
},
HashMap::new
)
)
.forEach((key, value) -> {
log.info("key :{}, value : {}", key, value.size());
});
}
key :null, value : 2
key :A, value : 2
key :B, value : 1
key :C, value : 1