Example usage for java.util TreeSet TreeSet

List of usage examples for java.util TreeSet TreeSet

Introduction

In this page you can find the example usage for java.util TreeSet TreeSet.

Prototype

public TreeSet() 

Source Link

Document

Constructs a new, empty tree set, sorted according to the natural ordering of its elements.

Usage

From source file:CityNavigator.java

public static void main(String[] args) {
    String[] cities = { "A", "B", "C", "D", "E", "F" };

    citiesSet = new TreeSet<String>();
    for (String city : cities)
        citiesSet.add(city);/*from ww  w  .j av  a2s. c o m*/

    for (String city : citiesSet.descendingSet())
        System.out.println("  " + city);
}

From source file:CityNavigator.java

public static void main(String[] args) {
    String[] cities = { "A", "B", "C", "D", "E", "F" };

    citiesSet = new TreeSet<String>();
    for (String city : cities)
        citiesSet.add(city);/*  ww w . j  a va  2  s  . c  om*/

    for (String city : citiesSet)
        System.out.println("  " + city);
}

From source file:TreeSetExample.java

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);
    }/* w ww .j a v  a 2 s  .c o m*/

    // 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);
    }

}

From source file:Main.java

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);//  www  . j a v a  2s .c om
    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));
}

From source file:Product.java

public static void main(String args[]) {
    Set<Product> prodList = new TreeSet<Product>();

    prodList.add(new Product("A", 1));
    prodList.add(new Product("B", 0));
    prodList.add(new Product("C", 2));
    prodList.add(new Product("D", 4));

    for (Product p : prodList)
        System.out.printf("%-14s ID: %d\n", p.prodName, p.prodID);
}

From source file:Product.java

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);
}

From source file:Main.java

public static void main(String[] args) {
    MyObject a1 = new MyObject("a", 1);
    MyObject a2 = new MyObject("a", 2);
    MyObject a3 = new MyObject("a", 3);
    MyObject b1 = new MyObject("b", 1);
    MyObject b2 = new MyObject("b", 2);
    MyObject b2a = new MyObject("b", 2);
    MyObject nullLevel = new MyObject("a", null);
    MyObject nullName = new MyObject(null, 2);
    SortedSet<MyObject> set = new TreeSet<MyObject>();
    set.add(a1);/*from   w  w w .ja  va 2  s  .  c om*/
    set.add(a2);
    set.add(a3);
    set.add(b1);
    set.add(b2);
    set.add(b2a);
    set.add(nullLevel);
    set.add(nullName);
    System.out.println(Arrays.toString(set.toArray()));
}

From source file:MyRange.java

public static void main(String[] args) {
    TreeSet<MyRange> set = new TreeSet<>();
    set.add(new MyRange(1, 0));
    set.add(new MyRange(2, 100));
    set.add(new MyRange(3, 500));
    set.add(new MyRange(4, 250));
    System.out.println(set.floor(new MyRange(50)));
    System.out.println(set.floor(new MyRange(300)));
    System.out.println(set.floor(new MyRange(600)));
}

From source file:SetDemo.java

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]);//from   w w w  . ja  v  a2s . co  m

    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());
}

From source file:Set2.java

public static void main(String[] args) {
    test(new HashSet());
    test(new TreeSet());
    test(new LinkedHashSet());
}