
import java.util.*;


public class Main // HelpSets
{

  // ***** main method *****
  public static void main(String[] arguments)
  {
      System.out.printf("Hello Sets - TreeSet and HashSet");
      System.out.printf("%n");
      System.out.printf("\n");

      // Stores data in an internal private object.
      // You can NOT USE [] to access the data!!!
      // There are NO INDEXES!!!!!

      // NO DUPLICATES!   
      // Use a TreeSet if you want your set sorted (ordered).
      // Use a HashSet if you don't need it ordered.
      
      // The TreeSet and HashSet will only hold objects, 
      // and for this case I have specified only Integer objects.

      // Example:
      // TreeSet<Integer> set1 = new TreeSet<Integer>();
      // HashSet<Integer> set2 = new HashSet<Integer>();
      
      // The new part creates a new TreeSet or HashSet object 
      // which holds the data.
      
      // The TreeSet<Integer>() calls the constructor to initialize
      // the instance variables of the TreeSet object.
      
      // The variable set1 will receive the memory address of the
      // TreeSet object.

      // The variable set2 will receive the memory address of the
      // HashSet object.
      
      TreeSet<Integer> set1 = new TreeSet<Integer>(); // or use <>
 
      System.out.println("Adding 5 " + set1.add(5));
      
      System.out.println("Adding 7 " + set1.add(7));

      System.out.println("Adding 7 " + set1.add(7));

      System.out.println("Adding 8 " + set1.add(8));
      
      System.out.println("set1 contains:");      
      System.out.println(set1); 
      System.out.println();

      System.out.println("set1 contains:");      
      System.out.println("contains 2 " + set1.contains(2)); 
      // System.out.println("indexOf " + set1.indexOf(8)); 
      System.out.println();

      System.out.println("Removing 2 " + set1.remove(2));
      System.out.println("Removing 7 " + set1.remove(7));
      
      
      // printing a set
      // You can call toString() or use a for each loop
      // REMEMBER THERE ARE NO INDEXES
      System.out.println("Printing the items in set1");
      for (Integer num : set1)
      {
          System.out.print(num + " ");
      }
      System.out.println();
      System.out.println();
 

      // You can also use an Iterator 
      // The Iterator object has three methods:
      // (there is also a ListIterator which has 
      //  additional methods)
      // hasNext() - boolean, true you have another object else false
      // next() - gives you a copy of the item (or memory address)
      // remove() - removes the item that you just retrieved.
      
      Iterator<Integer> it = set1.iterator();

      System.out.println("Printing the items in set1 with an Iterator");
      while (it.hasNext())
      {
          Integer num = it.next(); 
          System.out.print(num + " ");
      }
      
      it = set1.iterator();

      System.out.println("removing an item from set1");
      while (it.hasNext())
      {
          Integer num = it.next();
           
          if (num == 8)
          {
            it.remove();
          }
      }
      
      System.out.println();

      it = set1.iterator();

      System.out.println("Printing the items in set1 with an Iterator");
      while (it.hasNext())
      {
          Integer num = it.next(); 
          System.out.print(num + " ");
      }
      
      System.out.println();
      System.out.println();
      
  }



} // end of class HelpSets




