• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

HashSet

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*Dear Fellows, on compiling this program I am getting an output
C:\jdk1.2.1\bin>java SetApp
The size of the set is: 5
a
test
This
is
null
I think Output should have been
This
is
a
null
Test
Can any colleague kindly guide me, as to why the program output is not as I had expected it to be*/
import java.util.*;
public class SetApp {
public static void main(String args[]){
HashSet set = new HashSet();
set.add("This");
set.add("is");
set.add("is");
set.add("a");
set.add("a");
set.add(null);
set.add("test");
displaySet(set);
}
static void displaySet(HashSet set) {
System.out.println("The size of the set is: "+set.size());
Iterator i = set.iterator();
while(i.hasNext()){
Object o = i.next();
if(o == null) System.out.println("null");
else System.out.println(o.toString());
}
}
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HashSet does not make any guarantee about the order of objects because it uses hash codes to locate objects.
Bill
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YOu can use the Comparator function to order the elements in the HashSet by using Collections.sort or you can use Sortedset ...

Originally posted by ZEESHAN AZIZ:
/*Dear Fellows, on compiling this program I am getting an output
C:\jdk1.2.1\bin>java SetApp
The size of the set is: 5
a
test
This
is
null
I think Output should have been
This
is
a
null
Test
Can any colleague kindly guide me, as to why the program output is not as I had expected it to be*/
import java.util.*;
public class SetApp {
public static void main(String args[]){
HashSet set = new HashSet();
set.add("This");
set.add("is");
set.add("is");
set.add("a");
set.add("a");
set.add(null);
set.add("test");
displaySet(set);
}
static void displaySet(HashSet set) {
System.out.println("The size of the set is: "+set.size());
Iterator i = set.iterator();
while(i.hasNext()){
Object o = i.next();
if(o == null) System.out.println("null");
else System.out.println(o.toString());
}
}
}


 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic