Thursday, September 3, 2015

Java UTIL - HashSet - LinkedHashSet - TreeSet Examples

package com.util;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;

public class LinkedHashSetTreeDemo {
 public void operationHashSet()
 {
  HashSet hs = new HashSet();
       // add elements to the hash set

   hs.add("B");
   hs.add("B");
       hs.add("A");
       hs.add("D");
       hs.add("E");
       hs.add("C");
       hs.add("F");
       System.out.println("HASHSET ANY RANDOM OREDER"+hs);
 }
 public void operationLinkedHashSet()
 {
  LinkedHashSet hs = new LinkedHashSet();
       // add elements to the hash set
       hs.add("B");
       hs.add("B");
       hs.add("A");
       hs.add("D");
       hs.add("E");
       hs.add("C");
       hs.add("F");
       System.out.println("LINKED HASH SET SAME ORDER"+hs);
 }
 public void operationTreeSet()
 {
  TreeSet hs = new TreeSet();
       // add elements to the hash set

   hs.add("B");
   hs.add("B");
       hs.add("A");
       hs.add("D");
       hs.add("E");
       hs.add("C");
       hs.add("F");
       System.out.println("TREE SET"+hs);
 }
 public static void main(String args[]) {
       // create a hash set
  LinkedHashSetTreeDemo lhs= new LinkedHashSetTreeDemo();
  lhs.operationLinkedHashSet();
  lhs.operationHashSet();
  lhs.operationTreeSet();
    }
}