Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

LinkedHashMap demo?  RSS feed

 
Richard Hayward
Ranch Hand
Posts: 176
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone have a very simple example to show how a LinkedHashMap works in the case where the order is 'last access order'?
I've tried the following, but it results in a java.util.ConcurrentModificationException



Regards
Richard
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Hayward wrote:I've tried the following, but it results in a java.util.ConcurrentModificationException

Because get() updates the Map - if you think about it, it must do; otherwise how could it give you last accessed order?

Easiest is probably:
Winston
 
Richard Hayward
Ranch Hand
Posts: 176
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston, that's interesting. In the case of my test program



it does indeed show that the 'order' of the elements is changed after Buffalo is accessed at line 19.

Is accessing the values of m via an enhanced for statement guaranteed to always to get values in 'last accessed' ordering? The LinkedHashMap api docs describe it as a class
with predictable iteration order.

I was supposing that by 'iteration order' the docs mean the order in which values are returned by an Iterator, for example



Regards
Richard

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Hayward wrote:
with predictable iteration order.

I was supposing that by 'iteration order' the docs mean the order in which values are returned by an Iterator, for example


Correct, and the enhanced for loop is nothing more then a convenient way of using an iterator (the byte code actually does use the iterator). It is equivalent to:


The reason this works without affecting access order also comes from the LinkedHashMap API:
Invoking the put [or putAll] or get method results in an access to the corresponding entry ... No other methods generate entry accesses. In particular, operations on collection-views do not affect the order of iteration of the backing map.

Since the iterator is a view of the map, using the iterator does not affect the iteration order. What you were doing previously was using an iterator on a view of the keys, then using the get() method to get the value. The m.get() in your loop is what caused the concurrent modification exception (the get() would change the order, which could not be done while iterating).
 
Richard Hayward
Ranch Hand
Posts: 176
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Steve & Winston for your help.
I think I now understand how this works.

Regards
Richard
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!