-1

Could someone tell me how to get or print the String value of a map element? The below code results in "The method values() is undefined for the type String." I also tried .getValue() but the outcome is the same.

Thanks in advance!

Map<Integer, String> mapName = new HashMap<>();
mapName.put(0, "description_0");
mapName.put(1, "description_1");

for (Integer i : mapName.keySet()){
     System.out.println(mapName.get(i).values());
}

Varest
  • 57
  • 1
  • 11
  • Did you read the java doc about Map.get ? https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#get-java.lang.Object- – jhamon Feb 14 '20 at 13:47
  • 2
    Does this answer your question? [get string value from HashMap depending on key name](https://stackoverflow.com/questions/1789679/get-string-value-from-hashmap-depending-on-key-name) – jhamon Feb 14 '20 at 13:48
  • `for(String s : mapName.values()) System.out.println(s);` – Lino Feb 14 '20 at 13:51

2 Answers2

1

Answer provided by @Lino.

for(String s : mapName.values()) System.out.println(s);
Varest
  • 57
  • 1
  • 11
1

If you want to use stream:

mapName.entrySet().stream().forEach(elem-> System.out.println(elem));

This will allow you to use all the features of stream such as filtering,collecting , reducing etc. https://www.geeksforgeeks.org/stream-map-java-examples/