keySet () returns only the key set from your hash map, you must iterate this key set and get the value from the hash map using these keys.
In your example, the hashmap key type is TypeKey , but you specified TypeValue in a common for loop, so it cannot be compiled. You should change this to:
for (TypeKey name: example.keySet()){ String key = name.toString(); String value = example.get(name).toString(); System.out.println(key + " " + value); }
Update for Java8:
example.entrySet().forEach(entry->{ System.out.println(entry.getKey() + " " + entry.getValue()); });
If you do not need to print the key value and you just need the hashmap value, you can take advantage of the suggestions of others.
Another question: is this collection a zero base? I mean, if it has 1 key and value, will its size be 0 or 1?
The collection returned from keySet() is a Set. You cannot get a value from Set using an index, so the question is not whether it is zero or one. If your hash file has one key, the returned key keySet () will have one record inside and its size will be 1.
Ken Chan May 7 '11 at 9:13 AM 2011-05-07 09:13
source share