Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

LinkedHashSet

 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try

http://javaalmanac.com/egs/java.util/coll_SetOrder.html

Rob
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The example maintains the order of insertion but does not order elements based on last access, right?.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Lookout! Runaway whale! Hide behind this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic