Working with HashSet and TreeSet : Set : Collections Data Structure : Java examples (example source code) Organized by topic

Java
C++
PHP
Java Home »  Collections Data Structure   » [  Set  ]  Screenshots 
 



Working with HashSet and TreeSet



import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class ItemSet {
  public static void main(String args[]) {
    String names[] "Item 1""Item 2""Item 3"};
    Set moons = new HashSet();
    int namesLen = names.length;
    int index;
    for (int i = 0; i < 100; i++) {
      index = (int) (Math.random() * namesLen);
      moons.add(names[index]);
    }
    Iterator it = moons.iterator();
    while (it.hasNext()) {
      System.out.println(it.next());
    }
    System.out.println("---");
    Set orderedMoons = new TreeSet(moons);
    it = orderedMoons.iterator();
    while (it.hasNext()) {
      System.out.println(it.next());
    }
  }
}
           
       
Related examples in the same category
1.  Things you can do with Sets Things you can do with Sets
2.  Putting your own type in a Set Putting your own type in a Set
3.  Use set Use set
4.  Another Set demo
5.  Set substraction Set substraction
6.  TreeSet Demo TreeSet Demo
7.  Show the union and instersection of two sets
8.  Demonstrate the Set interface
9.  TreeSet Test
10.  Array Set extends AbstractSet Array Set extends AbstractSet
11.  Sync Test
12.  Set Copy
13.  Set and TreeSet
14.  Tail
15.  What you can do with a TreeSet What you can do with a TreeSet
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.