2

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",
    .....
}
2
  • If we want to have a List, then I am not sure this is possible. Would it be feasible to transform the List into a Map<String, List<Item>>, where the key is the type (as String) and the vallue is a List of all Items of this type? Commented May 22, 2021 at 12:00
  • @Turing85 Yes that would suffice Commented May 22, 2021 at 12:02

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

In my case Map<String, List<Item>> gave an error. Where I replaced it with Map<Object, List<Item>> itemsByTeam
Yeah now working for me. Eclipse gave me an error and suggested making the changes.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.