Java – update ArrayList with HashMap
•
Java
I just started learning to use HashMap and read the java tutorial, but I was in trouble
I'm trying to update the list in HashMap, but I want to get the list of the key. Is there a way to update the specific list of the key without making... 5 different lists and updating them?
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());
}
Solution
You can use something similar:
mMap.get("A").add("test");
mMap.get("B").add("entry");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
