在Java 6中如何实现Collectors.groupingBy的等效功能?

huangapple go评论65阅读模式
英文:

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&lt;UserVO&gt; <br/>
Each UserVO has a getCountry()

I want to group the List&lt;UserVO&gt; 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&lt;String, List&lt;UserVO&gt;&gt; studentsByCountry
= resultList.stream().collect(Collectors.groupingBy(UserVO::getCountry));
for (Map.Entry&lt;String, List&lt;UserVO&gt;&gt; entry: studentsByCountry.entrySet())
System.out.println(&quot;Student with country = &quot; + entry.getKey() + &quot; value are &quot; + entry.getValue());

I want output like a Map&lt;String, List&lt;UserVO&gt;&gt;:

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 &lt;String, List&lt;UserVO&gt;&gt; explicitly all the time.

Map&lt;String, List&lt;UserVO&gt;&gt; studentsByCountry = new HashMap&lt;String, List&lt;UserVO&gt;&gt;();
for (UserVO student: resultList) {
String country = student.getCountry();
List&lt;UserVO&gt; studentsOfCountry = studentsByCountry.get(country);
if (studentsOfCountry == null) {
studentsOfCountry = new ArrayList&lt;UserVO&gt;();
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&lt;String,List&lt;UserVO&gt;&gt; studentsByCountry = new TreeMap&lt;String,List&lt;UserVO&gt;&gt;(Collections.reverseOrder());

huangapple
  • 本文由 发表于 2020年4月9日 18:59:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/61119590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定