More elegant multi-condition groupby. #9719
(11I10I24) 活 动 中 心
1015770492 at qq.com
Sat Aug 6 09:49:18 UTC 2022
Hi, team
I have two suggestions
Firstly:
If we can provide a new API for grouping (java.util.stream.Collectors )
such as:
userList.stream().collect(Collectors.groupingBy(User::getName, User::getCity))
userList.stream().collect(Collectors.groupingBy("-", User::getName, User::getCity, User::getId));// key is "name-city", value is List after being grouped
It helps us to do grouping with less code. and It looks cooler for lambda.
And I've provided a simple demo.
demo: https://github.com/1015770492/CollectorsDemo/blob/master/src/main/java/com/example/demo/MultiGroupByDemo.java
issue: https://github.com/openjdk/jdk/pull/9719
Secondly:
similarly, if we can provide new api for sort with stream.
It can be:
userList.stream().sorted(User::getAge)
or userList.stream().sorted(User::getName)
or userList.stream().sorted(User::getName, User::getAge)
userList.stream().sorted(User::getAge) age is a Integer or int type ,it is Comparable, So if we can use the Function reference implements sort.
String implements the Comparable Interface but we do not use the features.
userList.stream().sorted(User::getCity, User::getName)
multi-condition sort we can transfor to String sort.
best regards
Yu, Jinhua
1015770492 at qq.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20220806/d07d282f/attachment.htm>
More information about the core-libs-dev
mailing list