• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LinkedHashSet

 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From K and B book

Optionally, you can construct a LinkedHashSet
so that it maintains the order in which elements were last accessed, rather than the
order in which elements were inserted.


How do I contruct such a LinkedHashSet?
 
Rob Harris
Greenhorn
Posts: 10
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example maintains the order of insertion but does not order elements based on last access, right?.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example maintains the order of insertion but does not order elements based on last access, right?.

Correct. As far as I can tell, there is no way to create a LinkedHashSet which uses access order. If your quote from Head First Java is accurate, I think that K&B may have confused LinkedHashSet with LinkedHashMap, which does have this option. (Use the new LinkedHashMap(int, float, boolean) constructor for this.) No such access-order capability was put into LinkedHashSet. I suggest submitting this to K&B via their web site. Or we could just see if one of them sees this...
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim for the clarification.

If your quote from Head First Java is accurate, I think that K&B may have confused LinkedHashSet with LinkedHashMap, which does have this option.


I have pasted directly from the ebook.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Yup - it's an error all right...

Funny thing is, I thought it had been corrected a few printings ago, I'm very sorry this one slipped through the cracks!

So, to clarify:

LinkedHashMap has a constructor that allows access via "last access",

LinkedHashSet does NOT :roll:

HTH,

Bert
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic