简体   繁体   English

如何正确结合Hashmap <String, ArrayList<Object> &gt;和HashMap <String, Object>

[英]How to union correctly Hashmap<String, ArrayList<Object>> and HashMap<String, Object>

I want to make my code clean add have the following trouble. 我想让我的代码干净添加有以下麻烦。 I want add 我想加

HashMap<String,Object> currentItem;

to my storage 到我的存储

HashMap<String, ArrayList<Object>> storage;

by the following way: 通过以下方式:

Add all Object of currentItem to ArrayList<Object> of storage according to the keys (they all are the same). 根据键将所有currentItem Object添加到storage ArrayList<Object> (它们都是相同的)。

This is my variant how to add currentItem to storage according to the keys 这是我的变种如何根据键将currentItem添加到存储

if (storage.containsKey("article_link")) 
{Object tmpObj;
    ArayList<Object> listTemp;
            tmpObj = currentItem.get("article_link");
        listTemp = storage.get("article_link");
        listTemp.add(tmpObj);
        storage.put("article_link", listTemp);

        tmpObj = o.get("image");
        listTemp = storage.get("image");
        listTemp.add(tmp);
        storage.put("image", rrr);
}

and this for each hashmap key. 这对每个hashmap键都有用。 I can write special function for repeating code, but I hope it can be solved more easier. 我可以编写重复代码的特殊功能,但我希望它可以更容易地解决。

Thanks. 谢谢。

One thing to note here is that, you don't need to put the mapping for new arraylist again, once you modify it. 这里需要注意的一点是,一旦修改它,就不需要再次为新的arraylist设置映射。 The changes will be reflected in the Map automatically, since, what you are having is the reference to the actual ArrayList object. 这些更改将自动反映在Map ,因为您所拥有的是对实际ArrayList对象的引用。

Secondly, you don't need to do all that hard-coding . 其次,您不需要进行所有hard-coding You can simply iterate over your currentItem map, and for each key-value pair, check the existence of key in storage , and if found, just update the corresponding List . 您可以简单地遍历currentItem映射,并为每个key-value对检查storage是否存在key ,如果找到,只需更新相应的List

Here's how you do that: - 这是你如何做到的: -

for (Entry<String, Object> entry: currentItem.entrySet()) {
    if (storage.containsKey(entry.getKey()) {
        storage.get(entry.getKey()).add(entry.getValue());

    } else {
        List<Object> newEntry = new ArrayList<Object>();
        newEntry.add(entry.getValue());
        storage.put(entry.getKey(), newEntry);
    }
}
for (Entry<String, Object> entry: currentItem.entrySet()) {
    if (!storage.containsKey(entry.getKey()) {
        storage.put(new ArrayList<Object>());
    }
    storage.get(entry.getKey()).put(entry.getValue());
}
Set<Entry<String,Object> entries = currentItem.entrySet();
Iterator<Entry<String,Object>> i = entries.iterator();
while(i.hasNext()) {
    Entry<String,Object> e = i.next();
    if(storage.containsKey(e.getKey()) { 
        storage.get(e.getKey()).add(e.getValue());
    }
    else {
        ArrayList l = new ArrayList();
        l.add(e.getValue());
        storage.put(e.getKey(),l);
    }
}

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

相关问题 如何将对象输入到ArrayList <HashMap<String,Object> &gt; - How to input the objects to a ArrayList <HashMap<String,Object>> 如何使用 stream 过滤器获取 hashmap 列表? ArrayList <hashmap<string, object> &gt; </hashmap<string,> - How use stream filter for list of hashmap? ArrayList<HashMap<String, Object>> 如何将 String 对象保存到 ArrayList 并删除此 ArrayList 中的 HashMap <HashMap<String, String> &gt;? - How to save String object into an ArrayList and also remove the HashMap inside of this ArrayList<HashMap<String, String>>? 数组列表 <HashMap<String,String> &gt;到JSON对象Java - ArrayList<HashMap<String,String>> to JSON object Java java:存储HashMap <String, String> 在ArrayList中 <Object> - java: Store HashMap<String, String> in ArrayList<Object> 要转换ArrayList <HashMap<String, Object> &gt;串 - Want to convert ArrayList<HashMap<String, Object>> to string 对Hashmap的ArrayList进行排序 <String,Object> 按整数值 - Sorting an ArrayList of Hashmap<String,Object> by int value 如何从ArrayList中删除dulpicates <HashMap<String,Object> &gt;在Java中? - How to remove the dulpicates from ArrayList<HashMap<String,Object>> in java? Java HashMap <String, ArrayList<Object[]> &gt;如何添加一个空数组? - Java HashMap<String, ArrayList<Object[]>> how to add an empty array? 如何从 ArrayList 中删除项目<HashMap<String, Object> &gt; - How to remove an item from ArrayList<HashMap<String, Object>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM