HashMap更新ArrayList

时间:2011-12-01 18:08:57

标签: java arraylist hashmap

我刚开始学习使用HashMap并阅读java教程,但我遇到了麻烦。

我正在尝试更新HashMap中的List,但我想获取该键的List,是否有办法更新密钥的特定List而不必制作... 5个不同的列表并更新那些?

 HashMap<String, ArrayList<String>> mMap = new HashMap<String, ArrayList<String>>();
        ArrayList<String> list = new ArrayList<String>();
        mMap.put("A", list);
        mMap.put("B", list);
        mMap.put("C", list);
        mMap.put("D", list);

        Iterator iter = mMap.entrySet().iterator();

        if (mMap.containsKey("A"))
        {   
            Map.Entry mEntry = (Map.Entry) iter.next();
            list.add("test");
            mMap.put("A",list);
            System.out.println(mEntry.getKey() + " : " + mEntry.getValue());
        }
        else if (mMap.containsKey("B"))
        {   
            Map.Entry mEntry = (Map.Entry) iter.next();
            list.add("entry");
            mMap.put("B",list);
            System.out.println(mEntry.getKey() + " : " + mEntry.getValue());
        }

6 个答案:

答案 0 :(得分:11)

您可以使用以下内容:

mMap.get("A").add("test");
mMap.get("B").add("entry");

答案 1 :(得分:6)

  1. 像@Tudor所说,使用mMap.get(“A”)。add(“foo”);

  2. 您将完全相同的列表放入每个地图条目中。您的初始行应为

  3.     mMap.put("A", new ArrayList());
        mMap.put("B", new ArrayList()); 
        ...
        mMap.put("Z", new ArrayList());
    

    另外,写一个即时检查的方法

    public synchronized void myAdd(String key, String value) {
       List<String> there = mMap.get(key);
       if (there == null) {
          there = new ArrayList<String>();
          mMap.put(key, there);
       }
       there.add(value);
    }
    

答案 2 :(得分:2)

你可能意味着:

HashMap<String, List<String>> mMap = new HashMap<String, List<String>>();
    mMap.put("A", new ArrayList<String>());
    mMap.put("B", new ArrayList<String>());
    mMap.put("C", new ArrayList<String>());
    mMap.put("D", new ArrayList<String>());

    if (mMap.containsKey("A"))
    {   
        mMap.get("A").add("test");
        System.out.println(mEntry.getKey() + " : " + mEntry.getValue());
    }
    else if (mMap.containsKey("B"))
    {   
        mMap.get("B").add("entry");
        System.out.println(mEntry.getKey() + " : " + mEntry.getValue());
    }
   ...

我想知道你是否真的需要那些containsKey检查! HTH!

答案 3 :(得分:1)

我认为在您的情况下,您可以使用Google GuavaMultimapListMultimap接口以及ArrayListMultimap实施。

Choosing right collection(在链接中只有标准集合,但Multimap在这种情况下是正确的)使代码更具可读性:

ListMultimap<String, String> myMultimap = ArrayListMultimap.create();
myMultimap.put("A", "test");
myMultimap.put("B", "entry");

然后myMultimap.get("A")为您提供一个列表(实际上为ArrayList个实例),其中包含一个元素:“test”,而myMultimap.get("C")则为您提供空列表。

Map<String, List<String>>方法相比:

  • 您不必使用空列表初始化“C”键,
  • 您没有null支票(没有NullPointerException s),
  • 您不必执行其他检查,例如myMap.containsKey("A")
  • 你写的代码更少,
  • 所以你的代码不容易出错,
  • 等等。

P.S。而不是:

HashMap<String, ArrayList<String>> mMap = new HashMap<String, ArrayList<String>>();

use interfaces, not classes when using collections即:

Map<String, List<String>> mMap = new HashMap<String, List<String>>();

甚至更好的Guava的“静态构造函数”(你不重复代码):

Map<String, List<String>> mMap = Maps.newHashMap()

当不需要有关实施的知识时。

答案 4 :(得分:1)

我会做这样的事情。

private final ReentrantLock lock = new ReentrantLock();
    Map<String ,List<String>> mMap=new HashMap<>();
    public  void myAdd(String key, String value) {
        try {
            lock.lock();
        List<String> there =mMap.get(key)==null?new ArrayList<>():mMap.get(key);
        there.add(value);
        mMap.put(key, there);
        }finally {
            lock.unlock();
        }
    }

答案 5 :(得分:0)

如果您使用不同的键添加相同的列表作为值,在您的情况下,键A,B,C和D都指向相同的列表,并通过一个键访问和更新列表,那么更改将是可见的在所有列表中。每个键指向相同的列表结构。

如果您希望列表不同,则需要对不同的密钥使用不同的密钥,您需要使用不同的列表。

你可以自动化这个过程,比如制作你自己的克隆给定列表的插入方法。