如何使用 Java 中的对象 属性 对列表中的对象进行分组
How to group objects in a list with object property in Java
如何将列表中的所有对象与相同的对象属性分组?不提及对象属性值。
型号Class:
public class Item {
private String id;
private String name;
private String team
}
List<item> items = new ArrayList();
我试过这个:
items.stream().filter(item -> "Elites".equals(item.team)).collect(Collectors.toList());
但这需要将团队名称作为参数传递。
如何在不指定团队值的情况下对项目进行分组?
并使用 Key 作为 项制作一个 HashMap。 team 和 value 作为 键值对列表 team.name & item.id
像这样:
"item.team":{
"item.id":"item.name",
"item.id":"item.name",
"item.id":"item.name",
.....
}
如果我们可以 return 一个 Map<String, List<Item>>
,其中键是 team
,值是属于该团队的 List<Item>
,我们可以使用
final Map<String, List<Item>> itemsByTeam =
items.stream().collect(Collectors.groupingBy(item -> item.team));
备注:此解决方案最初是 post 另一位用户在评论中发表的,他们很快就删除了评论。我不记得用户的名字。如果他们 post 一个答案,我会删除我的。如果他们不想 post 回答,但联系我,我会注明他们的姓名。
对代码的评论:我建议为属性引入 getter,因为 stream
操作最有可能在 class Item
本身之外调用,因此属性 team
将不可见。此外,这将导致像
这样的实现
final Map<String, List<Item>> itemsByTeam =
items.stream().collect(Collectors.groupingBy(Item::getTeam));
reader。
可能会或可能不会被视为“更令人愉快”
来自 Turing85 接受的答案。
我已经为我提出的问题创建了一个完整的解决方案。
创建具有以下结构的输出:
"item.team":{
"item.id":"item.name",
"item.id":"item.name",
"item.id":"item.name",
.....
}
源数据:
List<Item> itemsListData = //Get the data
对项目进行分组的函数:
public static Map<String, List<Item>> groupItemsByTeam(Collection<Item> itemsList) {
return itemsList.stream().collect(Collectors.groupingBy(Item::team));
}
构造 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("-------------------------------------------");
}
输出:
Team A :
Item 1= Item 1 name
Item 2= Item 2 name
-------------------------------------------
Team G :
Item 456= Item 456 name
Item 254= Item 254 name
-------------------------------------------
如何将列表中的所有对象与相同的对象属性分组?不提及对象属性值。
型号Class:
public class Item {
private String id;
private String name;
private String team
}
List<item> items = new ArrayList();
我试过这个:
items.stream().filter(item -> "Elites".equals(item.team)).collect(Collectors.toList());
但这需要将团队名称作为参数传递。
如何在不指定团队值的情况下对项目进行分组?
并使用 Key 作为 项制作一个 HashMap。 team 和 value 作为 键值对列表 team.name & item.id
像这样:
"item.team":{
"item.id":"item.name",
"item.id":"item.name",
"item.id":"item.name",
.....
}
如果我们可以 return 一个 Map<String, List<Item>>
,其中键是 team
,值是属于该团队的 List<Item>
,我们可以使用
final Map<String, List<Item>> itemsByTeam =
items.stream().collect(Collectors.groupingBy(item -> item.team));
备注:此解决方案最初是 post 另一位用户在评论中发表的,他们很快就删除了评论。我不记得用户的名字。如果他们 post 一个答案,我会删除我的。如果他们不想 post 回答,但联系我,我会注明他们的姓名。
对代码的评论:我建议为属性引入 getter,因为 stream
操作最有可能在 class Item
本身之外调用,因此属性 team
将不可见。此外,这将导致像
final Map<String, List<Item>> itemsByTeam =
items.stream().collect(Collectors.groupingBy(Item::getTeam));
reader。
可能会或可能不会被视为“更令人愉快”来自 Turing85 接受的答案。
我已经为我提出的问题创建了一个完整的解决方案。
创建具有以下结构的输出:
"item.team":{
"item.id":"item.name",
"item.id":"item.name",
"item.id":"item.name",
.....
}
源数据:
List<Item> itemsListData = //Get the data
对项目进行分组的函数:
public static Map<String, List<Item>> groupItemsByTeam(Collection<Item> itemsList) {
return itemsList.stream().collect(Collectors.groupingBy(Item::team));
}
构造 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("-------------------------------------------");
}
输出:
Team A :
Item 1= Item 1 name
Item 2= Item 2 name
-------------------------------------------
Team G :
Item 456= Item 456 name
Item 254= Item 254 name
-------------------------------------------