Source Code : Set, HashSet and TreeSet

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Set, HashSet and TreeSet

        

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

public class Main {

  public static void main(String args[]) {
    Set<String> hs = new HashSet<String>();

    hs.add("one");
    hs.add("two");
    hs.add("three");

    System.out.println("Here is the HashSet: " + hs);

    if (!hs.add("three"))
      System.out.println("Attempt to add duplicate. " + "Set is unchanged: " + hs);

    TreeSet<Integer> ts = new TreeSet<Integer>();

    ts.add(8);
    ts.add(19);
    ts.add(-2);
    ts.add(3);

    System.out.println(ts);

    System.out.println("First element in ts: " + ts.first());
    System.out.println("Last element in ts: " + ts.last());

    System.out.println("First element > 15: " + ts.higher(15));
    System.out.println("First element < 15: " + ts.lower(15));
  }
}

   
    
    
    
    
    
    
    
  

Thank with us