• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Collection(Set), Using a HashSet

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

In the following code, How come each time I print the set, am getting a sorted result?



Thanks.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the following code, How come each time I print the set, am getting a sorted result?



Really? When I run this code, my ordering is not sorted.... I get A, C, B.

And BTW, even if it was A, B, C, can it be attributed to dumb luck? Three values is certainly not enough to prove that the results is always sorted.

Henry
[ July 25, 2008: Message edited by: Henry Wong ]
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Um.. yeah but how come your getting A,C,B. Shouldn't you be getting C,A,B, the order which they were inserted?

Thanks.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hashset is neither ordered nor sorted. so when you access the variables the outcome is unpredictable. there was a good explanation of the difference between ordered and sorted in kate&bert.

BC
[ July 25, 2008: Message edited by: burak cetin ]
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Um.. sorted means 1,2,3,4 etc.. or a,b,c,d etc.. whats ordered?

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

Originally posted by Arjun Reddy:
Um.. sorted means 1,2,3,4 etc.. or a,b,c,d etc.. whats ordered?

Thanks.



Ordered means in what order like (First In First Out) or (Last In First Out) or any other way of inserting and retrieving from the Collection. But Set is not ordered, means it does not have any order of the above avtivities. If you get so called sorted output (a,b,c) its just a chance that for you it was like that (to confuse you more)
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it all depends on the hash function used by the JVM. Like for e.g if a hashCode() simply returns the ascii value of the letter, the output would be sorted
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Bajoria:
I think it all depends on the hash function used by the JVM. Like for e.g if a hashCode() simply returns the ascii value of the letter, the output would be sorted



Not really. First, it depends on how the buckets are implemented. The Set may not map linearly with hashcode. Second, there may not be a one to one mapping between buckets and hashcodes. Two different letter may end up in the same bucket, then it will depend on how each bucket is implemented.

Henry
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One other point, if something is not sorted or ordered, it doesn't mean that the ordering random. Don't expect iterations through the Set to yield different results -- it may, but it may not. The point is, any ordering is purely coincidental, and the HashSet collection has not been implemented with any ordering.

Henry
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys.
reply
    Bookmark Topic Watch Topic
  • New Topic