我有一个 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