【发布时间】:2020-04-09 10:59:09
【问题描述】:
我有一个List<UserVO>
每个 UserVO 都有一个 getCountry()
我想根据getCountry() 对List<UserVO> 进行分组
我可以通过流来做到这一点,但我必须在 Java6 中做到这一点
这是在 Java8 中。我想要这个在 Java6 中
Map<String, List<UserVO>> studentsByCountry
= resultList.stream().collect(Collectors.groupingBy(UserVO::getCountry));
for (Map.Entry<String, List<UserVO>> entry: studentsByCountry.entrySet())
System.out.println("Student with country = " + entry.getKey() + " value are " + entry.getValue());
我想要像Map<String, List<UserVO>>这样的输出:
CountryA - UserA, UserB, UserC
CountryB - UserM, User
CountryC - UserX, UserY
编辑:我可以进一步重新调整这个Map,以便根据国家/地区的 displayOrder 显示。显示顺序为 countryC=1, countryB=2 & countryA=3
比如我要显示
CountryC - UserX, UserY
CountryB - UserM, User
CountryA - UserA, UserB, UserC
【问题讨论】: