Does the code below guarantee print keys by insertion order :
LinkedHashMap<String, String> m = new LinkedHashedMap<String,String>();
m.put("A", "Test 1");
m.put("C", "Test 2");
m.put("B", "Test 3");
for (String key : m.keySet()) {
System.out.println(key);
}
Does the it always print A, C and then B? Or do I need to use an iterator to guarantee that I access the collection in insertion order?
The reason I'm confused is that according to the docs, keySet()
returns a Set
and there's no order guaranteed when iterating over a Set
. Also the keySet()
method is inherited from HashMap
, so I wonder how LinkedHashMap
's keySet()
guarantees keys return in insertion order when it is inherited from HashMap
.