0

I have a Map with values and get a Set using Map.keySet method. In this code:

Map<String, String> map = new HashMap<>(); 
map.put("1", "a"); 
map.put("2", "b"); 
map.put("3", "c"); 
Set<String> set = map.keySet();

for (int i = 0; i < 5; i++) {
    for (String key : set) {
        System.out.println(key);
    } 
}

am I guaranteed to get

1
2
3

written out every time? Where is this guarantee written down ? In Javadoc?

EDIT: Actually I don't care about the insertion order, but I care about the fact that using for-each loop on a set will produce the same result over and over, providing that the undelying map does not change (I don't call put, remove).

kovica
  • 2,443
  • 3
  • 21
  • 25

5 Answers5

0

Use LinkedHashMap if you want to retrieve in order in which you put key .

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
0

No, you are not. But you can use LinkedHashMap (http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html) and then you will be guaranteed.

asenovm
  • 6,397
  • 2
  • 41
  • 52
0

LinkedHashMap for order of additionn (put), and TreeMap (interface SortedMap) for order of keys.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I was just thinking that reusing a set object in for-each loop will provide the same result over an over, but I figured it out that the set is still backed by a map. So changing the map also changes the set. – kovica Mar 05 '13 at 10:41
0

Unfortunately the docs for HashMap state that keySet() method does not return a SortedSet, it just returns a Set, for which the ordering is not guaranteed.

See HashMap.keySet()

Read, in particular: It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time.

vikingsteve
  • 38,481
  • 23
  • 112
  • 156
0

No you're not guaranteed a specific order, unless you use a HashMap which implements a custom set that can give you this guarantee. The Set the HashMap gives you back have an Iterator() method which iterates over the elements in "no particular order".

Read the java documentation: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Set.html#iterator()

If you want the guarantee that the elements are iterated over in-order, i.e. ascending order, use something that implements SortedMap like TreeMap.

TreeMap Documentation: http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html

On this page you find the getSet() method which says "The set's iterator returns the keys in ascending order".

Lurvas777
  • 35
  • 6
  • Again, I don't want any particular order, I just want the result is the same over multiple invocainion of the for-each loop – kovica Mar 05 '13 at 10:50
  • If you loop one time you would get the order you wrote. But if you add an element to the **HashMap** and then loop through it again, you might get that element in between the others in the loop. It has to do with the *.hashcode()* method. You can't be sure were the new elements get put in the **HashMap**. – Lurvas777 Mar 05 '13 at 11:07
  • You are correct, but I know where and when the underlying map is changed. It is my code. – kovica Mar 05 '13 at 12:23
  • then it's no problem, if you know what output you'll get. – Lurvas777 Mar 05 '13 at 13:59