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:NavigableSetDemo.java

public static void main(String[] args) {
    List<Integer> list = Arrays.asList(3, 2, 4, 1, 5);
    NavigableSet<Integer> ns = new TreeSet<Integer>(list);
    System.out.println("Ascending order (default): " + ns);

    Iterator<Integer> descendingIterator = ns.descendingIterator();
    StringBuilder sb = new StringBuilder("Descending order: ");
    while (descendingIterator.hasNext()) {
        int m = descendingIterator.next();
        sb.append(m + " ");
    }/*from ww w . ja  v  a2s. co  m*/
    System.out.println(sb);

    int greatest = ns.lower(3);
    System.out.println("Lower of 3 = " + greatest);

    int smallest = ns.higher(3);
    System.out.println("Higher of 3 = " + smallest);
}

From source file:MyComp.java

public static void main(String args[]) {
    TreeSet<String> ts = new TreeSet<String>(new MyComp());

    ts.add("C");/*ww  w. j a va  2s  .  c o m*/
    ts.add("A");
    ts.add("B");
    ts.add("E");
    ts.add("F");
    ts.add("D");

    for (String element : ts)
        System.out.print(element + " ");
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String elements[] = { "I", "P", "E", "G", "P" };
    SortedSet set = new TreeSet(Arrays.asList(elements));
    System.out.println(set.tailSet("I"));
    System.out.println(set.headSet("I"));
    System.out.println(set.headSet("I\0"));
    System.out.println(set.tailSet("I\0"));
    System.out.println(set.subSet("I", "P\0"));
    System.out.println(set.subSet("I", "I\0"));
    System.out.println(set.subSet("I", "I"));
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String elements[] = { "S", "P", "E", "G", "P" };
    Set set = new TreeSet(Collections.reverseOrder());
    for (int i = 0, n = elements.length; i < n; i++) {
        set.add(elements[i]);/*from w ww  . ja va 2s  . co  m*/
    }
    System.out.println(set);
    System.out.println(((TreeSet) set).comparator());
}

From source file:MyComparator.java

public static void main(String args[]) {
    TreeSet<String> ts = new TreeSet<String>(new MyComparator());

    ts.add("java2s.com");
    ts.add("A");/*from  www  .  j  av  a  2  s .c  om*/
    ts.add("B");
    ts.add("E");
    ts.add("F");
    ts.add("D");

    for (String element : ts)
        System.out.print(element);

    System.out.println();
}

From source file:Main.java

public static void main(String[] args) {
    Map<Integer, Integer> data = new HashMap<Integer, Integer>();
    data.put(10, 2);/*  ww  w .ja v a 2 s  .  c  o  m*/
    data.put(20, 3);
    data.put(30, 5);
    data.put(40, 15);
    data.put(50, 4);

    SortedSet<Integer> keys = new TreeSet<Integer>(data.keySet());
    String s = "";
    for (Integer key : keys) {
        s += key + " : ";
        for (int i = 0; i < data.get(key); i++) {
            s += "*";
        }
        s += "\n";
    }
    System.out.println(s);
}

From source file:SortedSetDemo.java

public static void main(String[] args) {
    SortedSet sortedSet = new TreeSet(Arrays.asList("one two three four five six seven eight".split(" ")));
    System.out.println(sortedSet);
    Object low = sortedSet.first(), high = sortedSet.last();
    System.out.println(low);/*from  www  .  j av  a  2s  .c o  m*/
    System.out.println(high);
    Iterator it = sortedSet.iterator();
    for (int i = 0; i <= 6; i++) {
        if (i == 3)
            low = it.next();
        if (i == 6)
            high = it.next();
        else
            it.next();
    }
    System.out.println(low);
    System.out.println(high);
    System.out.println(sortedSet.subSet(low, high));
    System.out.println(sortedSet.headSet(high));
    System.out.println(sortedSet.tailSet(low));
}

From source file:MyObject.java

public static void main(String[] args) {
    Set<MyObject> set = new TreeSet<>(new Comparator<MyObject>() {
        @Override//from   w  ww .  ja  v  a 2 s. c om
        public int compare(MyObject left, MyObject right) {
            return left.value - right.value;
        }
    });
    set.add(new MyObject(1));
    set.add(new MyObject(2));
    set.add(new MyObject(3));
    set.add(new MyObject(1)); // '1' is already in set
    System.out.println("size:" + set.size());// print 3
    System.out.println(set.remove(new MyObject(3)));
    System.out.println("size:" + set.size());
}

From source file:MyComparator.java

public static void main(String args[]) {
    TreeSet<String> ts = new TreeSet<String>(new MyComparator());

    ts.add("C");//from   w w w.  j a  v  a  2  s .  c o m
    ts.add("A");
    ts.add("B");
    ts.add("E");
    ts.add("F");
    ts.add("D");

    for (String element : ts)
        System.out.print(element + " ");

    System.out.println();
}

From source file:is.java

  public static void main(String[] args) {
  List<Integer> list = Arrays.asList(3, 2, 4, 1, 5);
  NavigableSet<Integer> ns = new TreeSet<Integer>(list);
  System.out.println("Ascending order (default): " + ns);

  Iterator<Integer> descendingIterator = ns.descendingIterator();
  StringBuilder sb = new StringBuilder("Descending order: ");
  while (descendingIterator.hasNext()) {
    int m = descendingIterator.next();
    sb.append(m + " ");
  }/*from  w w w.j  a  v a  2s  .  com*/
  System.out.println(sb);

  int greatest = ns.lower(3);
  System.out.println("Lower of 3 = " + greatest);

  int smallest = ns.higher(3);
  System.out.println("Higher of 3 = " + smallest);
}