-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());
}

3

2 Answers 2

1

Answer provided by @Lino.

for(String s : mapName.values()) System.out.println(s);
Sign up to request clarification or add additional context in comments.

Comments

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/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.