[英]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?如何将具有相同 Object 属性的列表中的所有对象分组? Without mentioning the Object property value.无需提及 Object 属性值。
Model Class: 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.并以Key为项目制作HashMap 。 team and value as a list of key-value pairs with that team.name & item.id team和value作为具有该 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如果我们可以返回一个Map<String, List<Item>>
,其中键是team
,值是属于该团队的List<Item>
,我们可以使用
final Map<String, List<Item>> itemsByTeam =
items.stream().collect(Collectors.groupingBy(item -> item.team));
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.对代码的评论:我建议为属性引入 getter,因为stream
操作最有可能在 class Item
本身之外调用,因此属性team
将不可见。 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 .从Turing85接受的答案。
I have created a complete solution for the Question I asked.我为我提出的问题创建了一个完整的解决方案。
To create an output with the following structure:创建具有以下结构的 output:
"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: Function 对项目进行分组:
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 :构造由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: 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
-------------------------------------------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.