问题
i have this hashmap with string and arraylis:
Map<String, ArrayList<String>> devNameType = new HashMap<String, ArrayList<String>>();
public void kimenetPV(String name){
//létrehozom a nevet
String devName = name;
//létrehozom a kimeneteket tartalmazó arraylistet
ArrayList<String> outputs = new ArrayList<String>();
// 2. hozzáadaom az elemeket
outputs.add("EM A");
outputs.add("EM B");
outputs.add("AOUT1");
outputs.add("AOUT2");
devNameType.put(devName, outputs);
Iterator iter = devNameType.entrySet().iterator();
//kilistázom az elemeket
while (iter.hasNext()) {
Map.Entry mEntry = (Map.Entry) iter.next();
System.out.println(mEntry.getKey() + " : " + mEntry.getValue());
}
}
how can i print all the values? Sorry for the begginer question i'm using the hashmaps for first time.
回答1:
You can print the key:value pairs as this:
Iterator<Entry<String, List<String>>> iter = devNameType.entrySet().iterator();
while (iter.hasNext()) {
Entry<String, List<String>> next = iter.next();
List<String> value = next.getValue();
System.out.println("key = " + next.getKey());
System.out.println("values : ");
for (String str : value) {
System.out.println(str);
}
}
回答2:
for (List<String> value : devNameType.values()) {
... do something with this value
}
For key/value:
for (String key: devNameType.keySet()) {
List<String> values = devNameType.get(key);
... do something with the name and values
}
See http://docs.oracle.com/javase/6/docs/api/java/util/Map.html#values() and http://docs.oracle.com/javase/6/docs/api/java/util/Map.html#keySet()
回答3:
With hashMap.values() you get a Collection of all values: http://developer.android.com/reference/java/util/HashMap.html#values()
来源:https://stackoverflow.com/questions/19495155/how-to-get-hashmap-values-depends-on-key