Skip to content
Advertisement

Does using an Iterator over a TreeMap in Java interfere with the order of keys?

I want to know that if using an Iterator over a TreeMap in Java interfere with the order of keys?

Advertisement

Answer

From http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html#keySet() :

Returns a Set view of the keys contained in this map. The set’s iterator returns the keys in ascending order.

If for some reason you need the keys in descending order, you can use descendingKeySet()

If you want to iterate over the values, you can use entrySet()

(Wow, I didn’t know that Map isn’t a Collection before…)

9 People found this is helpful
Advertisement