How to iterate over a TreeMap? [duplicate]
Asked Answered
A

4

178

Possible Duplicate:
How do I iterate over each Entry in a Map?

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?

Amery answered 23/8, 2009 at 16:42 Comment(1)
@Click: "... for all 'key's which have a particular value ...". Do you mean for all keys in a given Set, or for all keys that satisfy a given predicate?Scrubby
W
275

Assuming type TreeMap<String,Integer> :

for(Map.Entry<String,Integer> 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)

Wenda answered 23/8, 2009 at 16:50 Comment(5)
FYI, EntrySet is the preferred way to iterate through any Map since EntrySets are by specification reflective and always represent the state of data in the Map even if the Map underneath would change.Galina
Adding to Zed's answer , using entrySet will give the user the power to delete a particular entry while iterating.Dawna
Giving the name treeMap to your TreeMap threw me off at first.Genesis
will this give out the values ordered?Sporades
No. TreeMap order by Key value.Crocodile
K
37
    //create TreeMap instance
    TreeMap treeMap = new TreeMap();

    //add key value pairs to TreeMap
    treeMap.put("1","One");
    treeMap.put("2","Two");
    treeMap.put("3","Three");

    /*
      get Collection of values contained in TreeMap using
      Collection values()        
    */
    Collection c = treeMap.values();

    //obtain an Iterator for Collection
    Iterator itr = c.iterator();

    //iterate through TreeMap values iterator
    while(itr.hasNext())
      System.out.println(itr.next());

or:

   for (Map.Entry<K,V> entry : treeMap.entrySet()) {
        V value = entry.getValue();
        K key = entry.getKey();
   }

or:

   // Use iterator to display the keys and associated values
   System.out.println("Map Values Before: ");
   Set keys = map.keySet();
   for (Iterator i = keys.iterator(); i.hasNext();) {
     Integer key = (Integer) i.next();
     String value = (String) map.get(key);
     System.out.println(key + " = " + value);
   }
Kiln answered 23/8, 2009 at 16:47 Comment(1)
There must be a more performant way that do another treemap lookup here: "(String) map.get(key)". You already know the element at this stage, just curious.Doud
S
9

Just to point out the generic way to iterate over any map:

 private <K, V> void iterateOverMap(Map<K, V> map) {
    for (Map.Entry<K, V> entry : map.entrySet()) {
        System.out.println("key ->" + entry.getKey() + ", value->" + entry.getValue());
    }
    }
Shlomo answered 30/8, 2012 at 4:31 Comment(0)
O
3

Using Google Collections, assuming K is your key type:

Maps.filterKeys(treeMap, new Predicate<K>() {
  @Override
  public boolean apply(K key) {
    return false; //return true here if you need the entry to be in your new map
  }});

You can use filterEntries instead if you need the value as well.

Olag answered 23/8, 2009 at 17:3 Comment(1)
This doesn't do any sorting.Balbinder

© 2022 - 2024 — McMap. All rights reserved.