Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Ordering in HashSet

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I read in K&B book that HashSet does not maintain the order of elements. But I have the following program:

public class ListClass {
public static void main(String[] args){
Set set = new HashSet();
set.add("Hari");
set.add("Ganesh");
set.add("Giri");
set.add("Garry");
for(int i=0;i<30;i++){
for(Object o:set){
System.out.print(o+" ");
}
System.out.println("");
}
set.add("Latha");
set.add("Zeta");
set.add("Narayan");
for(int i=0;i<30;i++){
for(Object o:set){
System.out.print(o+" ");
}
System.out.println("");
}
}
}
which produces the same output over and over again. Can anyone please explain why this is so?
 
Ranch Hand
Posts: 140
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i dont know the explanation for this. but what you see is still an unordered iteration of the elements, it just happens to come out the same when there are few elements in the set.

try adding more elements(100 or more) the unordered iteration will be more obvious.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The concept of a Set is that it is a collection of objects without any ordering. You can see it as a bag of objects, and the objects are all jumbled together in the bag. There is no inherent order.

This does not mean that a certain implementation of Set (such as HashSet) can't store objects in a certain order. The thing you have to keep in mind that nothing is specified or guaranteed about the order, so you should not write a program that relies on the order of objects in a HashSet.

So, the fact that a Set has no order does not necessarily mean that the objects have to be presented in a random order.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using this statement ?
for(int i=0;i<30;i++){
The statement for(Object o:set){ itself will print the contents of the hashset.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vipin Das:
Why are you using this statement ?
for(int i=0;i<30;i++){
The statement for(Object o:set){ itself will print the contents of the hashset.


Probably because he wants to demonstrate that if he prints the contents of the HashSet 30 times, he gets the same ordering of the elements each time.

As I said, there's nothing wrong with that; there is no requirement anywhere that the elements have to be in a different, random order each time.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. That's very true Jesper.

All what it says is "Set does NOT maintain any order". That does NOT mean that always it has to produce the results in a different order!.

The order of insertion is also one of the random orders!
 
Ganesha Kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic