• 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

Question on HashSet & LinkedHashSet

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The above code prints
4
onetwofourthree
If I replace HashSet with LinkedHashSet it gives same o/p
Then what is the difference between HashSet & LinkedHashSet?Doesn't LinkedHashSet maintain the order by insertion order?
Thanks
Veena
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Set means duplicates are not allowed. If you print out the values returned by your add methods then you will see that add("one") returns false the second time. LinkedHashSet is just a subclass of HashSet and both implements Set interface. Hope this helps.
Deep
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Deep,
Thanks for reeplieng.
But,my doubt was ,for LinkedHashSet shouldn't the o/p be "onetwothreefour"?as it maintains the order by insertion order?
Veena
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Veena,
i am not sure what it will actually print, but conceptually, yes the LinkedHashSet should print it as onetwothreefour. there is a very interesting link which i found regarding this - http://www-106.ibm.com/developerworks/java/library/j-mer0821/
take a look of it. it is quiet good.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Veena Point:
But,my doubt was ,for LinkedHashSet shouldn't the o/p be "onetwothreefour"?as it maintains the order by insertion order?


Yes, that should be the output. Can you show us how you modified your program to use LinkedHashSet?
[ September 14, 2003: Message edited by: Alton Hernandez ]
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess HashSet orders its elements by hashCode of the object it stores.

While LinkedHashCode will print by insertion or update order.
 
reply
    Bookmark Topic Watch Topic
  • New Topic