java HashMap<Key,List<Object>> 来自包含相同属性的对象列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31522608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HashMap<Key,List<Object>> from list of objects containing same property
提问by Niraj Adhikari
I have a List of objects.
我有一个对象列表。
eg>
例如>
Object 1
对象 1
groupId=1 name=name1
groupId=1 名称=名称 1
Object 2
对象 2
groupId=1 name=name2
groupId=1 名称=名称2
Object 3
对象 3
groupId=1 name=name3
groupId=1 名称=名称 3
Object 4
对象 4
groupId=2 name=name4
groupId=2 名称=名称 4
Multiple objects in the List have same value for groupId
. I need to create Sub-Lists of objects with same groupId
. How do I do this in Java.
列表中的多个对象具有相同的 值groupId
。我需要创建具有相同groupId
. 我如何在 Java 中执行此操作。
My Inital thought was to create a HashMap<Integer,List<Object>>
but i am unsure about indefinite values of groupIds
coming in , which in turn makes me unsure about how to group objects with same groupIds
together with groupId
as the hashmap's key.
我最初的想法是创建一个,HashMap<Integer,List<Object>>
但我不确定传入的不确定值groupIds
,这反过来又使我不确定如何将具有相同对象的对象与散列图的键groupIds
组合在一起groupId
。
If the groupId's were not to change or increase in the future i could have iterated over the original list and written a switch statement to create required arrays.
如果 groupId 将来不会更改或增加,我可以遍历原始列表并编写 switch 语句来创建所需的数组。
回答by assylias
With Java 8 you could use the groupingBy
collector:
在Java 8,你可以使用的groupingBy
收集器:
Map<String, List<MyObject>> map = list.stream()
.collect(Collectors.groupingBy(MyObject::getGroupId));
回答by Niraj Adhikari
I did it this way. I used LinkedHashMap<K,V>
as i had to preserve the order of my objects i had sorted according to object's priority value property.
我是这样做的。我使用LinkedHashMap<K,V>
因为我必须保留我根据对象的优先级值属性排序的对象的顺序。
LinkedHashMap<Group, List<Object>> hashMap=new LinkedHashMap<Group, List<Object>>();
for(Object model:objects){
if(!hashMap.containsKey(model.getGroup())){
List<Object> list= new ArrayList<Object>();
list.add(model);
hashMap.put(Group,list);
}
else{
hashMap.get(model.getGroup()).add(model);
}
回答by Razib
I think you need a Map like this -
我想你需要这样的地图 -
Map map = new HashMap<Integer, List<String>>();
Where the key
in the HashMap
is Integer
represents your groupId
(ie. - 1, 2, 3 etc) and value
(ie - name1, name2 etc) of the HashMap
stored at the List
of String
.
凡key
在HashMap
是Integer
代表你的groupId
(即- 1,2,3等)和value
-所述的(即1,名称2等)HashMap
存储在所述List
的String
。
You may use the following steps -
您可以使用以下步骤 -
1.Iterate over your four lists.
2.Add each item to a Map
construction (eg.- here map
) based on the key groupId.
1.迭代你的四个列表。
2.根据密钥 groupId将每个项目添加到Map
构造(例如-此处map
)。
I think this linkwould be helpful in this context.
我认为此链接在这种情况下会有所帮助。
回答by Buru
In Java 8you can use some filters.
在Java 8 中,您可以使用一些过滤器。
public class A {
public int groupId;
public String name;
public A(int groupId, String name) {
this.groupId = groupId;
this.name = name;
}
}
Filter using Collections API
使用 Collections API 过滤
ArrayList<A> list=new ArrayList();
list.add(new A(2, "tom"));
list.add(new A(2, "rome"));
list.add(new A(1, "sam"));
List<A> filteredlist = list.stream().filter(p -> p.groupId==2).collect(Collectors.toList());