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(1);
    treeadd.add(2);//  ww w.  j  a  v  a  2  s.  co m
    treeadd.add(3);
    treeadd.add(4);
    treeadd.add(5);
    treeadd.add(6);
    treeadd.add(7);
    treeadd.add(8);

    TreeSet<Integer> treetailsetincl = (TreeSet<Integer>) treeadd.tailSet(5, true);

    Iterator<Integer> iterator = treetailsetincl.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(2);// w  w  w.ja  va2 s  . c om
    treeadd.add(3);
    treeadd.add(4);
    treeadd.add(5);
    treeadd.add(6);
    treeadd.add(7);
    treeadd.add(8);

    TreeSet<Integer> treesubset = (TreeSet<Integer>) treeadd.subSet(3, 7);

    Iterator<Integer> iterator = treesubset.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(2);//from ww w  . ja  va 2  s . co m
    treeadd.add(3);
    treeadd.add(4);
    treeadd.add(5);
    treeadd.add(6);
    treeadd.add(7);
    treeadd.add(8);

    TreeSet<Integer> treetailset = (TreeSet<Integer>) treeadd.tailSet(4);

    Iterator<Integer> iterator = treetailset.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(2);//from w  ww .ja v  a  2 s .  com
    treeadd.add(3);
    treeadd.add(4);
    treeadd.add(5);
    treeadd.add(6);
    treeadd.add(7);
    treeadd.add(8);

    TreeSet<Integer> treesubset = (TreeSet<Integer>) treeadd.subSet(3, true, 7, true);

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

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

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 w  w w .j a v a2s  .  com*/
    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) {
    TreeSet<Integer> tree = new TreeSet<Integer>(Collections.reverseOrder());

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

    // using comparator
    System.out.println(tree.comparator());

}

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);
    ts.add(19);/*w ww.ja  va 2  s . c om*/
    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:MyComparator.java

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

    ts.add("C");
    ts.add("A");/*  w w  w  . j  a v a2  s.c o  m*/
    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) {

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

    System.out.println(treeadd.isEmpty());

    treeadd.add(2);

    System.out.println(treeadd.isEmpty());
}

From source file:Main.java

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