Example usage for java.util TreeSet add

List of usage examples for java.util TreeSet add

Introduction

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

Prototype

public boolean add(E e) 

Source Link

Document

Adds the specified element to this set if it is not already present.

Usage

From source file:Main.java

public static void main(String[] args) {

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

    treeadd.add(12);
    treeadd.add(13);//from w ww.  j a v  a2 s  .c o  m
    treeadd.add(14);
    treeadd.add(15);

    Iterator<Integer> iterator = treeadd.iterator();

    while (iterator.hasNext()) {
        System.out.print(iterator.next());
    }
}

From source file:Main.java

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

    tree.add(12);
    tree.add(13);//from   www .j a v a  2s  .  c  o m
    tree.add(14);

    // cloning tree into clinetree
    TreeSet<Integer> clonetree = (TreeSet) tree.clone();

    // creating iterator
    Iterator<Integer> iterator = clonetree.iterator();

    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}

From source file:Main.java

public static void main(String[] args) {

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

    treeadd.add(1);
    treeadd.add(13);//from   www .  j  a  v  a2 s.com
    treeadd.add(17);
    treeadd.add(2);

    // create descending iterator
    Iterator<Integer> iterator = treeadd.descendingIterator();

    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}

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

public static void main(String[] args) {

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

    treeadd.add(1);
    treeadd.add(3);//from  ww  w . j a  v a2s. c o m
    treeadd.add(17);
    treeadd.add(2);

    System.out.println("Last element is: " + treeadd.pollLast());

    Iterator<Integer> iterator = treeadd.iterator();

    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}

From source file:Main.java

public static void main(String[] args) {

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

    treeadd.add(1);
    treeadd.add(3);//from  w  w w.j  av a2 s  .c  o  m
    treeadd.add(17);
    treeadd.add(2);

    System.out.println("First element is: " + treeadd.pollFirst());

    Iterator<Integer> iterator = treeadd.iterator();

    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}

From source file:Main.java

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

    tree.add(12);
    tree.add(13);// ww  w . j a  v  a2s .com
    tree.add(14);
    tree.add(15);
    tree.add(16);
    tree.add(17);

    // getting values less than 15
    TreeSet<Integer> treeheadset = (TreeSet) tree.headSet(15);

    Iterator<Integer> iterator = treeheadset.iterator();

    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}

From source file:Main.java

public static void main(String[] args) {

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

    treeadd.add(12);
    treeadd.add(13);//w w  w  .j  a  v a2 s.c o m
    treeadd.add(14);
    treeadd.add(15);

    Iterator<Integer> iterator = treeadd.iterator();

    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }

    treeadd.clear();
    System.out.println(treeadd.size());
}

From source file:MyComp.java

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

    ts.add("C");
    ts.add("A");/*from   ww  w. ja v a  2s  .  co  m*/
    ts.add("B");
    ts.add("E");
    ts.add("F");
    ts.add("D");

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

From source file:Main.java

public static void main(String[] args) {

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

    treeadd.add(1);
    treeadd.add(13);//from   w w  w  .  j a v a2s . c  o  m
    treeadd.add(17);
    treeadd.add(2);

    // creating reverse set
    TreeSet<Integer> treereverse = (TreeSet<Integer>) treeadd.descendingSet();

    // create descending set
    Iterator<Integer> iterator = treereverse.iterator();

    //Tree set data in reverse order
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}