I try to use java 8 features.
I have a class
@Data
@NoArgsConstructor
@AllArgsConstructor
class User {
private String pId;
private String uId;
private String price;
}
I have a list, I try to group by pId
and count the non null uId
and price
. Example:
List<User> list =
Arrays.asList(
new User ("p1", "u1", null),
new User ("p1", "u2", "a"),
new User ("p2", null, "b"),
new User ("p2", null, "c"),
new User ("p3", "u4", "d")
);
My expected output is
[
{ pId:"p1", uCount:2, priceCount:1 },
{ pId:"p2", uCount:0, priceCount:2 },
{ pId:"p3", uCount:1, priceCount:1 }
]
I tried like following
Map<String, Map<Object, Long>> collect =
list.stream()
.collect(
Collectors.groupingBy(
User ::getPId,
Collectors.groupingBy(f -> f.getUId(), Collectors.counting())));
My final mapping class is
@Data
@NoArgsConstructor
@AllArgsConstructor
class Stat {
private String pId;
private Integer uCount;
private Integer priceCount;
}
Since I'm new to java, I'm facing struggle to complete it, I tried my best. Is that possible to remove null filed and count?