Wednesday, November 13, 2019

java - How to iterate over a TreeMap?












I want to iterate over a TreeMap, and for all keys which have a particular value, I want them to be added to a new TreeMap. How can I do this?


Answer



Assuming type TreeMap :



for(Map.Entry entry : treeMap.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();

System.out.println(key + " => " + value);
}



(key and Value types can be any class of course)


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...