Computer Science Canada

Sets

Author:  kevin Noronha [ Tue Mar 11, 2008 2:33 pm ]
Post subject:  Sets

what code would you use to print out contents from a set
Example:
HashSet set=new HashSet();
set.add(5);


How can you print out the value 5??

Author:  HeavenAgain [ Tue Mar 11, 2008 3:59 pm ]
Post subject:  RE:Sets

depends on how you want to work this, there is a contains method, which you can check if 5 is in there, so we do
code:
if (set.contains(5))
    System.out.println(5);
//or use the toString method
System.out.println(set);
//or converting it to array, and treat it like an array
set.toArray();
any more? I'm not sure... if there is a better answer, let me know Smile

Author:  McKenzie [ Tue Mar 11, 2008 4:39 pm ]
Post subject:  Re: Sets

With a HashSet you can can test if 5 is in your set e.g.
set.contains(5)

You can also print everything like:
for(Integer i : set)
System.out.println(i);


: