It is mentioned in the LinkedHashMap Javadocs:
In particular, operations on collection-views do not affect the order of iteration of the backing map.
What does "operations on collection-views" mean?
The collection-views of Map
s are the objects returned by:
Map#keySet()
Map#values()
Map#entrySet()
Operations on those simply means any method calls on the collection-view instance.
This test is supposed to demonstrate how it works
Map m = new LinkedHashMap(16, 0.75f, true);
m.put(1, 1);
m.put(2, 2);
m.put(3, 3);
System.out.println(m);
m.get(2);
System.out.println(m);
Set keys = m.keySet(); //API: Returns a Set view of the keys contained in this map.
keys.iterator().next();
System.out.println(m);
output
{1=1, 2=2, 3=3}
{1=1, 3=3, 2=2}
{1=1, 3=3, 2=2}
that is, accessing entry 2-2 changed the iteration order, and accessing the first entry 1-1 on the keySet view did not
If I understand well, maybe I don't, the collection-view are the Collection
s obtained by its abstractions, like entrySet, values and keySet.
Operations in these sets will not affect the order of access of the objects inside yours LinkedHashMap
when you're using the special constructor that makes your objects be order by access-order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With