英文:
How to do Collectors.groupingBy equivalent in Java 6?
问题
// Java 6 code
List<UserVO> userList = new ArrayList<UserVO>(); // Your list of UserVO objects
Map<String, List<UserVO>> studentsByCountry = new HashMap<String, List<UserVO>>();
// Grouping the users by country
for (UserVO user : userList) {
String country = user.getCountry();
if (!studentsByCountry.containsKey(country)) {
studentsByCountry.put(country, new ArrayList<UserVO>());
}
studentsByCountry.get(country).add(user);
}
// Printing the grouped data
for (Map.Entry<String, List<UserVO>> entry : studentsByCountry.entrySet()) {
System.out.print(entry.getKey() + " - ");
List<UserVO> countryUsers = entry.getValue();
for (int i = 0; i < countryUsers.size(); i++) {
System.out.print(countryUsers.get(i).getName()); // Assuming UserVO has a getName() method
if (i < countryUsers.size() - 1) {
System.out.print(", ");
}
}
System.out.println();
}
// Shuffling the map based on display order
List<String> displayOrder = Arrays.asList("CountryC", "CountryB", "CountryA");
Map<String, List<UserVO>> shuffledMap = new LinkedHashMap<String, List<UserVO>>();
for (String country : displayOrder) {
if (studentsByCountry.containsKey(country)) {
shuffledMap.put(country, studentsByCountry.get(country));
}
}
// Printing the shuffled map
for (Map.Entry<String, List<UserVO>> entry : shuffledMap.entrySet()) {
System.out.print(entry.getKey() + " - ");
List<UserVO> countryUsers = entry.getValue();
for (int i = 0; i < countryUsers.size(); i++) {
System.out.print(countryUsers.get(i).getName());
if (i < countryUsers.size() - 1) {
System.out.print(", ");
}
}
System.out.println();
}
Please note that the above code assumes you have a class named UserVO
with appropriate methods like getCountry()
and getName()
for retrieving the country and name of a user respectively. Additionally, it assumes that you have the list of UserVO
objects named userList
. The code first groups the users by country and then shuffles the map based on the specified display order.
英文:
I have a List<UserVO>
<br/>
Each UserVO has a getCountry()
I want to group the List<UserVO>
based on its getCountry()
<br/>
I can do it via streams but I have to do it in Java6 <br/><br/>
This is in Java8. I want this in Java6 <br/>
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());
I want output like a Map<String, List<UserVO>>
:
CountryA - UserA, UserB, UserC
CountryB - UserM, User
CountryC - UserX, UserY
Edit: Can I further reschuffle this Map
so that I display according to the displayOrder of the countries. Display order is countryC=1, countryB=2 & countryA=3
For example I want to display
CountryC - UserX, UserY
CountryB - UserM, User
CountryA - UserA, UserB, UserC
答案1
得分: 4
这是您在普通Java中的做法。请注意,Java 6不支持菱形操作符,因此您必须始终明确地使用<String,List<UserVO>>
。
Map<String, List<UserVO>> studentsByCountry = new HashMap<String, List<UserVO>>();
for (UserVO student : resultList) {
String country = student.getCountry();
List<UserVO> studentsOfCountry = studentsByCountry.get(country);
if (studentsOfCountry == null) {
studentsOfCountry = new ArrayList<UserVO>();
studentsByCountry.put(country, studentsOfCountry);
}
studentsOfCountry.add(student);
}
使用流会更简短,对吗?所以尝试升级到Java 8!
要根据反向字母顺序的特定顺序,如评论中所述,您可以将第一行替换为以下内容:
Map<String, List<UserVO>> studentsByCountry = new TreeMap<String, List<UserVO>>(Collections.reverseOrder());
英文:
This is how you do it with plain Java. Please note that Java 6 doesn't support the diamond operator so you have use <String, List<UserVO>>
explicitly all the time.
Map<String, List<UserVO>> studentsByCountry = new HashMap<String, List<UserVO>>();
for (UserVO student: resultList) {
String country = student.getCountry();
List<UserVO> studentsOfCountry = studentsByCountry.get(country);
if (studentsOfCountry == null) {
studentsOfCountry = new ArrayList<UserVO>();
studentsByCountry.put(country, studentsOfCountry);
}
studentsOfCountry.add(student);
}
It's shorter with streams, right? So try to upgrade to Java 8!
To have a specific order based on the reversed alphabetical String, as mentioned in the comments, you can replace the first line with the following:
Map<String,List<UserVO>> studentsByCountry = new TreeMap<String,List<UserVO>>(Collections.reverseOrder());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论