简体   繁体   English

哈希图 <Key,List<Object> &gt;来自包含相同属性的对象列表

[英]HashMap<Key,List<Object>> from list of objects containing same property

I have a List of objects. 我有一个对象列表。

eg> 例如

Object 1 对象1

groupId=1 name=name1 groupId = 1名称= name1

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名称= name4

Multiple objects in the List have same value for groupId . 列表中的多个对象具有相同的groupId值。 I need to create Sub-Lists of objects with same groupId . 我需要创建具有相同groupId的对象的子列表。 How do I do this in Java. 如何在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. 我Inital的想法是创建一个HashMap<Integer,List<Object>>但我不确定的不定值groupIds进来,这又使我不能确定与同如何组对象groupIds一起groupId作为HashMap中的关键。

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语句以创建所需的数组。

With Java 8 you could use the groupingBy collector : 在Java 8中,您可以使用groupingBy收集器

Map<String, List<MyObject>> map = list.stream()
               .collect(Collectors.groupingBy(MyObject::getGroupId));

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);
        }

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 . 其中keyHashMapInteger代表您groupId (即- 1,2,3等)和value -对的(1,名称等IE) HashMap存储在ListString

You may use the following steps - 您可以使用以下步骤-

1. Iterate over your four lists. 1.遍历您的四个列表。
2. Add each item to a Map construction (eg.- here map ) based on the key groupId. 2.根据键groupId将每个项目添加到Map构造(例如-here map )中。

I think this link would be helpful in this context. 我认为此链接在这种情况下会有所帮助。

In Java 8 you 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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