简体   繁体   中英

How to group objects in a list with object property in Java

How to group all the Objects in a List with the same Object property? Without mentioning the Object property value.

Model Class:

public class Item {
  private String id;
  private String name;
  private String team
}

List<item> items = new ArrayList();

I have tried this:

items.stream().filter(item -> "Elites".equals(item.team)).collect(Collectors.toList());

But this requires passing the team name as a parameter.

How can we group the items without specifying a team value?

And Making a HashMap with Key as the item. team and value as a list of key-value pairs with that team.name & item.id

Like this:

"item.team":{
    "item.id":"item.name",
    "item.id":"item.name",
    "item.id":"item.name",
    .....
}

If we can return a Map<String, List<Item>> , where the key is the team and the value is a List<Item> belonging to that team, we can use

final Map<String, List<Item>> itemsByTeam = 
    items.stream().collect(Collectors.groupingBy(item -> item.team));

Ideone demo

Remark: This solution was first posted in a comment by another user and they deleted the comment shortly after. I do not remember the user's name. If they post an answer, I will delete mine. If they do not want to post an answer, but contact me, I will credit them by name.

A comment on the code: I would recommend to introduce getters for the attributes since the stream -operation is most likely to be called outside of class Item itself, hence attribute team will not be visible. Also, this would lead to an implementation like

final Map<String, List<Item>> itemsByTeam = 
    items.stream().collect(Collectors.groupingBy(Item::getTeam));

which may or may not be regarded as "more pleasing" to the reader.

From the accepted answer by Turing85 .

I have created a complete solution for the Question I asked.

To create an output with the following structure:

"item.team":{
    "item.id":"item.name",
    "item.id":"item.name",
    "item.id":"item.name",
    .....
}

Source data:

List<Item> itemsListData = //Get the data

Function to group the Items:

public static Map<String, List<Item>> groupItemsByTeam(Collection<Item> itemsList) {
    return itemsList.stream().collect(Collectors.groupingBy(Item::team));
}

Structure the items list returned by groupItemsByTeam :

//GET THE GROUPED DATA
Map<String, List<Item>> result = groupItemsByTeam(itemsListData);

//STRUCTURE THE GROUPED DATA
for (Entry<String, List<Item>> parentItem : result .entrySet()) {
    System.out.println(parentItem .getKey() + " : "); // item.team value

    for (Item childItem : parentItem.getValue()) {
        System.out.println(childItem.getKEY() + " = " + childItem.getVALUE());
    }
    System.out.println("-------------------------------------------");
}

OUTPUT:

Team A : 
Item 1= Item 1 name
Item 2= Item 2 name
-------------------------------------------
Team G : 
Item 456= Item 456 name
Item 254= Item 254 name
-------------------------------------------

Reference from baeldung.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM