Java/Collections Data Structure/TreeSet

Материал из Java эксперт
Перейти к: навигация, поиск

Add values to TreeSet<String>

   
import java.util.TreeSet;
class TreeSetDemo {
  public static void main(String args[]) {
    TreeSet<String> ts = new TreeSet<String>();
    ts.add("C");
    ts.add("A");
    ts.add("B");
    ts.add("E");
    ts.add("F");
    ts.add("D");
    System.out.println(ts);
  }
}





Check if a particular value exists in Java TreeSet example

  
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    TreeSet<String> tSet = new TreeSet<String>();
    tSet.add("1");
    tSet.add("2");
    tSet.add("3");
    tSet.add("4");
    tSet.add("5");
    boolean blnExists = tSet.contains("3");
    System.out.println("3 exists in TreeSet ? : " + blnExists);
  }
}





Comparable with a sorted collection.

 
import java.util.Set;
import java.util.TreeSet;
class Product implements Comparable<Product> {
  String prodName;
  int prodID;
  Product(String str, int id) {
    prodName = str;
    prodID = id;
  }
  public int compareTo(Product p2) {
    return prodName.rupareToIgnoreCase(p2.prodName);
  }
  public boolean equals(Object p2) {
    return prodName.rupareToIgnoreCase(((Product) p2).prodName) == 0;
  }
}
public class Main {
  public static void main(String args[]) {
    Set<Product> prodList = new TreeSet<Product>();
    prodList.add(new Product("A", 13546));
    prodList.add(new Product("B", 04762));
    prodList.add(new Product("C", 12221));
    prodList.add(new Product("D", 44387));
    for (Product p : prodList)
      System.out.printf("%-14s ID: %d\n", p.prodName, p.prodID);
  }
}





Copy all elements of Java TreeSet to an Object Array Example

  
       
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    TreeSet<Integer> tSet = new TreeSet<Integer>();
    tSet.add(new Integer("1"));
    tSet.add(new Integer("2"));
    tSet.add(new Integer("3"));
    Object[] objArray = tSet.toArray();
    for (Object obj: objArray){
      System.out.println(obj);
    }
  }
}





Creating a Sorted Set

 
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class Main {
  public static void main(String[] argv) {
    // Create the sorted set
    Set<String> set = new TreeSet<String>();
    set.add("b");
    set.add("c");
    set.add("a");
    Iterator it = set.iterator();
    while (it.hasNext()) {
      Object element = it.next();
      System.out.println(element);
    }
    // Create an array containing the elements in a set
    String[] array = (String[]) set.toArray(new String[set.size()]);
    Arrays.toString(array);
  }
}





Get Head Set from Java TreeSet example

  
import java.util.SortedSet;
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    TreeSet<String> tSet = new TreeSet<String>();
    tSet.add("1");
    tSet.add("2");
    tSet.add("3");
    tSet.add("4");
    tSet.add("5");
    SortedSet sortedSet = tSet.headSet("3");
    System.out.println("Head Set Contains : " + sortedSet);
  }
}





Get lowest and highest value stored in Java TreeSet

  
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    TreeSet<String> tSet = new TreeSet<String>();
    tSet.add("1");
    tSet.add("2");
    tSet.add("3");
    tSet.add("4");
    tSet.add("5");
    System.out.println("Lowest value Stored in Java TreeSet is : " + tSet.first());
    System.out.println("Highest value Stored in Java TreeSet is : " + tSet.last());
  }
}





Get Size of Java TreeSet

  
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    TreeSet<Integer> tSet = new TreeSet<Integer>();
    System.out.println("Size of TreeSet : " + tSet.size());
    tSet.add(new Integer("1"));
    tSet.add(new Integer("2"));
    tSet.add(new Integer("3"));
    System.out.println(tSet.size());
    // remove one element from TreeSet using remove method
    tSet.remove(new Integer("1"));
    System.out.println("Size of TreeSet after removal : " + tSet.size());
  }
}





Get Sub Set from Java TreeSet

  
import java.util.TreeSet;
import java.util.SortedSet;
public class Main {
  public static void main(String[] args) {
    TreeSet<String> tSet = new TreeSet<String>();
    tSet.add("1");
    tSet.add("2");
    tSet.add("3");
    tSet.add("4");
    tSet.add("5");
    
    SortedSet sortedSet = tSet.subSet("2", "5");
    System.out.println("SortedSet Contains : " + sortedSet);
  }
}





Get Synchronized Set from Java TreeSet example

  
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    TreeSet treeSet = new TreeSet();
    Set set = Collections.synchronizedSet(treeSet);
  }
}





Get Tail Set from Java TreeSet

  
import java.util.SortedSet;
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    TreeSet<String> tSet = new TreeSet<String>();
    tSet.add("1");    
    tSet.add("2");
    tSet.add("3");
    tSet.add("5");
    tSet.add("4");
    SortedSet sortedSet = tSet.tailSet("2");
    System.out.println("Tail Set Contains : " + sortedSet);
  }
}





Iterate through elements of Java TreeSet

  
import java.util.Iterator;
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    TreeSet<Integer> tSet = new TreeSet<Integer>();
    tSet.add(new Integer("1"));
    tSet.add(new Integer("2"));
    tSet.add(new Integer("3"));
    Iterator itr = tSet.iterator();
    while (itr.hasNext()){
      System.out.println(itr.next());
    }
  }
}





Loop and remove element from a TreeSet

  
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetExample {
  public static void main(String[] args) {
    // Create a new TreeSet
    Set<Integer> set = new TreeSet<Integer>();
    // Add Items to the TreeSet
    set.add(new Integer(1));
    set.add(new Integer(2));
    set.add(new Integer(3));
    set.add(new Integer(4));
    set.add(new Integer(5));
    set.add(new Integer(6));
    set.add(new Integer(7));
    set.add(new Integer(8));
    set.add(new Integer(9));
    set.add(new Integer(10));
    // Use iterator to display the vsetes
    System.out.println("TreeSet Before: ");
    for (Iterator i = set.iterator(); i.hasNext();) {
      Integer integer = (Integer) i.next();
      System.out.println(integer);
    }
    // Remove the integer 6
    System.out.println("\nRemove Integer 6");
    set.remove(new Integer(6));
    // Use iterator to display the vsetes
    System.out.println("\nTreeSet After: ");
    for (Iterator i = set.iterator(); i.hasNext();) {
      Integer integer = (Integer) i.next();
      System.out.println(integer);
    }
  }
}





Remove all elements from Java TreeSet

  
       
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    TreeSet<Integer> tSet = new TreeSet<Integer>();
    tSet.add(new Integer("1"));
    tSet.add(new Integer("2"));
    tSet.add(new Integer("3"));
    System.out.println("TreeSet before removal : " + tSet);
    tSet.clear();
    System.out.println("TreeSet after removal : " + tSet);
    System.out.println("Is TreeSet empty ? " + tSet.isEmpty());
  }
}





Remove specified element from Java TreeSet

  
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    TreeSet<Integer> tSet = new TreeSet<Integer>();
    tSet.add(new Integer("1"));
    tSet.add(new Integer("2"));
    tSet.add(new Integer("3"));
    System.out.println(tSet);
    System.out.println("Was 2 removed from TreeSet ? " + tSet.remove(new Integer("2")));
    System.out.println(tSet);
  }
}





Sort items in a Set

  
import java.util.Set;
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    Set<String> set = new TreeSet<String>();
    set.add("Z");
    set.add("A");
    set.add("F");
    set.add("B");
    set.add("H");
    set.add("X");
    set.add("N");
    for (String item : set) {
      System.out.print(item + " ");
    }
  }
}





TreeSet, SortedSet and HashSet

  
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
class SetDemo {
  public static void main(String[] args) {
    String[] letters = { "A", "B", "C", "D", "E" };
    Set s = new HashSet();
    for (int i = 0; i < letters.length; i++)
      s.add(letters[i]);
    if (!s.add(letters[0]))
      System.out.println("Cannot add a duplicate.\n");
    Iterator iter = s.iterator();
    while (iter.hasNext())
      System.out.println(iter.next());
    System.out.println("");
    SortedSet ss = new TreeSet();
    for (int i = 0; i < letters.length; i++)
      ss.add(letters[i]);
    if (!ss.add(letters[0]))
      System.out.println("Cannot add a duplicate.\n");
    iter = ss.iterator();
    while (iter.hasNext())
      System.out.println(iter.next());
  }
}