• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Using Character and Strings with HashSet ,LinkedHashSet,HashMap,LinkedHashMap Problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output of hashset and linkedhashset incase of Character as input is

a.unsorted,unordered(hashset)

b.ordered by insertion order(linkedhashset)



output:


[d, s, c, a] hashset
d
s
c
a
[s, d, c, a] linkedhashset
s
d
c
a


But,the output of hashSet and LinkedHashSet is same when strings are used as input



output:
[car, abc, zsd] hashset
car
abc
zsd
[car, abc, zsd] linkedhashset
car
abc
zsd



Why is the output same in case of Strings ?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, in your case, it happens to be same. Because, When you iterate through a HashSet, the order is unpredictable, while a LinkedHashSet lets you iterate through the elements in the order in which they were inserted. Have look on the below code.


The output: [BCD, ABC, CDFG].
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very similar to this.
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic