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(SortedSet<E> s) 

Source Link

Document

Constructs a new tree set containing the same elements and using the same ordering as the specified sorted set.

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String elements[] = { "A", "C", "D", "G", "F" };
    TreeSet set = new TreeSet(Arrays.asList(elements));
    System.out.println(set);//  w ww . j a va2s. c o m
    System.out.println(set.first());
    System.out.println(set.last());
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String elements[] = { "A", "C", "D", "G", "F" };
    Set set = new TreeSet(Collections.reverseOrder());
    for (int i = 0, n = elements.length; i < n; i++) {
        set.add(elements[i]);//from  w w w.j  av a  2 s. c  o  m
    }
    System.out.println(set);
    System.out.println(((TreeSet) set).comparator());
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String elements[] = { "A", "C", "D", "G", "F" };
    TreeSet<String> set = new TreeSet<String>(Collections.reverseOrder());
    for (int i = 0, n = elements.length; i < n; i++) {
        set.add(elements[i]);/*ww w  . j  a va2  s .  com*/
    }
    System.out.println(set);
    System.out.println(((TreeSet) set).comparator());
}

From source file:Main.java

public static void main(String[] args) {
    // Sort the names based on their length, placing null first
    SortedSet<String> names = new TreeSet<>(Comparator.nullsFirst(Comparator.comparing(String::length)));
    names.add("XML");
    names.add("CSS");
    names.add("HTML");
    names.add(null); // Adds a null

    // Print the names
    names.forEach(System.out::println);

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String elements[] = { "A", "C", "D", "G", "F" };
    Set set = new TreeSet(Collections.reverseOrder());
    for (int i = 0, n = elements.length; i < n; i++) {
        set.add(elements[i]);/*  ww  w . j  a  v a  2 s.c om*/
    }
    System.out.println(set);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {

    String elements[] = { "A", "C", "D", "G", "F" };
    Set set = new TreeSet(Arrays.asList(elements));
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
        System.out.println(iter.next());
    }/*from   w  w w .j a v  a2s.c  o m*/
}

From source file:Main.java

public static void main(String[] args) {
    SortedSet<Person> personsById = new TreeSet<>(Comparator.comparing(Person::getId));

    personsById.add(new Person(1, "X"));
    personsById.add(new Person(2, "Z"));
    personsById.add(new Person(3, "A"));
    personsById.add(new Person(4, "C"));
    personsById.add(new Person(4, "S")); // A duplicate Person

    System.out.println("Persons by  Id:");
    personsById.forEach(System.out::println);

    SortedSet<Person> personsByName = new TreeSet<>(Comparator.comparing(Person::getName));
    personsByName.add(new Person(1, "X"));
    personsByName.add(new Person(2, "Z"));
    personsByName.add(new Person(3, "A"));
    personsByName.add(new Person(4, "C"));

    System.out.println("Persons by  Name: ");
    personsByName.forEach(System.out::println);

}

From source file:Main.java

public static void main(String a[]) {
    // using name comparator (String comparison)
    TreeSet<Empl> nameComp = new TreeSet<Empl>(new MyNameComp());
    nameComp.add(new Empl("A", 3));
    nameComp.add(new Empl("J", 6));
    nameComp.add(new Empl("C", 2));
    nameComp.add(new Empl("T", 2));
    for (Empl e : nameComp) {
        System.out.println(e);// ww w .j  a  v  a 2s . co  m
    }
    // using salary comparator (int comparison)
    TreeSet<Empl> salComp = new TreeSet<Empl>(new MySalaryComp());
    salComp.add(new Empl("A", 3));
    salComp.add(new Empl("J", 6));
    salComp.add(new Empl("C", 2));
    salComp.add(new Empl("T", 2));
    for (Empl e : salComp) {
        System.out.println(e);
    }
}

From source file:Main.java

public static void main(String a[]) {
    // By using name comparator (String comparison)
    TreeSet<Empl> nameComp = new TreeSet<Empl>(new MyNameComp());
    nameComp.add(new Empl("R", 3));
    nameComp.add(new Empl("J", 6));
    nameComp.add(new Empl("C", 20));
    nameComp.add(new Empl("T", 2));

    System.out.println(nameComp);

    // By using salary comparator (int comparison)
    TreeSet<Empl> salComp = new TreeSet<Empl>(new MySalaryComp());
    salComp.add(new Empl("R", 3));
    salComp.add(new Empl("J", 6));
    salComp.add(new Empl("C", 20));
    salComp.add(new Empl("T", 2));
    System.out.println(salComp);//w  w  w.  j a va2  s. co m

}

From source file:Tail.java

public static void main(String args[]) throws Exception {
    String elements[] = { "Irish Setter", "Poodle", "English Setter", "Gordon Setter", "Pug" };
    SortedSet set = new TreeSet(Arrays.asList(elements));
    System.out.println(set.tailSet("Irish Setter"));
    System.out.println(set.headSet("Irish Setter"));
    System.out.println(set.headSet("Irish Setter\0"));
    System.out.println(set.tailSet("Irish Setter\0"));
    System.out.println(set.subSet("Irish Setter", "Poodle\0"));
    System.out.println(set.subSet("Irish Setter", "Irish Setter\0"));
    System.out.println(set.subSet("Irish Setter", "Irish Setter"));
}