TreeSet Methods In Java With Examples

TreeSet is a type of Collection, that implements Set Interface. In TreeSet all the values are stored in there natural order, like all integer values are stored in ascending order and strings are stored according to Dictionary values. Apart from adding this functionality of maintaining natural ordering, TreeSet do not allow null values.


TreeSet Methods in JAVA:

Let us discuss all the TreeSet methods one by one with Examples in Java.

1. boolean add(Object o):

This methods adds element in the object, which will automatically stores all the elements in increasing order, as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

  TreeSet <String>treeSetObject2 = new TreeSet<String>();

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(5);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  treeSetObject2.add("B");

  treeSetObject2.add("C");

  treeSetObject2.add("A");

  treeSetObject2.add("D");

  treeSetObject2.add("A");

  System.out.println("Values in TreeSet object 1: " +treeSetObject1);

  System.out.println("Values in TreeSet object 2:  " +treeSetObject2);

  }

}

Output:

Values in TreeSet object 1: [1, 2, 3, 5]

Values in TreeSet object 2: [A, B, C, D]

2. boolean addAll(Collection c):

This method adds all the elements of one object to another , as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(5);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  treeSetObject2.add(9);

  treeSetObject2.add(11);

  treeSetObject2.add(1);

  treeSetObject2.add(8);

  treeSetObject2.add(7);

  treeSetObject1.addAll(treeSetObject2);

  System.out.println("Values in TreeSet object 1: " +treeSetObject1);

  }

}

Output:

Values in TreeSet object 1: [1, 2, 3, 5, 7, 8, 9, 11]

3. Object ceiling(Object o):

This method retrieves the least element which is greater than or equal to the given element in the argument list, or null if there is no such element in the set as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(5);

  treeSetObject1.add(1);

System.out.println("Ceiling value of '5' is:" +treeSetObject1.ceiling(5));

System.out.println("Ceiling value of '2' is:" +treeSetObject1.ceiling(2));

 }

}

Output:

Ceiling value of '5' is:5

Ceiling value of '2' is:3

4. void clear():

This method removes all of the elements from this object, as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(5);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  System.out.println("Values in TreeSet object 1:" +treeSetObject1);

  treeSetObject1.clear();

  System.out.println("Values in TreeSet object 1:" +treeSetObject1);

  }

}

Output:

Values in TreeSet object 1:[1, 2, 3, 5]

Values in TreeSet object 1:[]

5. Object clone():

This method returns a copy of set, as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(5);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  treeSetObject2=(TreeSet<Integer>) treeSetObject1.clone();

  System.out.println("Values in Cloned TreeSet:" +treeSetObject2);

  }

}

Output:

Values in Cloned TreeSet:[1, 2, 3, 5]

6. Comparator comparator():

This method returns the object of Comparator Class which is used to order the elements, or it returns null if it is using natural ordering as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(5);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  treeSetComparator= (TreeSet<Integer>) treeSetObject1.comparator();

  System.out.println("Value in Tree Set Comparator: " +treeSetComparator);

  }

}

Output:

Value in Tree Set Comparator: null

7. boolean contains(Object o):

This method returns true if specified element is present in the given set, as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(5);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  System.out.println("Checking if Tree Set Object contains element '0' : "

+treeSetObject1.contains(0));

  System.out.println("Checking if Tree Set Object contains element '1' : "

+treeSetObject1.contains(1));

  }

}

Output:

Checking if Tree Set Object contains element '0' : false

Checking if Tree Set Object contains element '1' : true

8. Iterator descendingIterator():

This method retrieves an object of Iterator class which is set in decreasing order of the elements in the Set, as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(5);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  Iterator iteratorobj;

  iteratorobj = treeSetObject1.descendingIterator();

  while (iteratorobj.hasNext())
  {

    System.out.println(iteratorobj.next() + " ");

  }

 }

}

Output:

5

3

2

1

9. NavigableSet descendingSet():

This method returns a all the elements in Decreasing order, as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(5);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  treeSetObject2= (TreeSet<Integer>) treeSetObject1.descendingSet();

  System.out.println("Values in Reverse order: " +treeSetObject2);

  }

}

Output:

Values in Reverse order: [5, 3, 2, 1]

10. Object first():

This method returns the lowest element in the Set, as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(5);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  System.out.println("Lowest element in the tree set is : " +treeSetObject1.first());

  }

}

Output:

Lowest element in the tree set is : 1

11. Object floor(Object o):

This method returns the greatest element which is less than or equal to the given element in the argument list, or null if there is no such element as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(5);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  System.out.println("Floor value of '4' : " +treeSetObject1.floor(4));

  System.out.println("Floor value of '5' : " +treeSetObject1.floor(5));

  }

}

Output:

Floor value of '4' : 3

Floor value of '5' : 5

12. SortedSet  headSet(Object toElement):

This method returns group of elements that are strictly less than toElement as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  treeSetObject2=(TreeSet<Integer>) treeSetObject1.headSet(4);

  System.out.println("Values in Tress Set Head Set : " + treeSetObject2 );

  }

}

Output:

Values in Tress Set Head Set : [1, 2, 3]

13. NavigableSet headSet(Object toElement, boolean inclusive):

This method Returns group of elements that are less than (or equal to, if inclusive is true) toElement as mentioned in the argument list, as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

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

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  treeSetObject2=(TreeSet<Integer>) treeSetObject1.headSet(4, true);

  treeSetObject3=(TreeSet<Integer>) treeSetObject1.headSet(4, false);

  System.out.println("Values in Tress Set Head Set in object 2: " + treeSetObject2 );

  System.out.println("Values in Tress Set Head Set in object 3: " + treeSetObject3 );

  }

}

Output:

Values in Tress Set Head Set in object 2: [1, 2, 3, 4]

Values in Tress Set Head Set in object 3: [1, 2, 3]

14. Object higher(Object o):

This method returns the least element in the TreeSet which is strictly greater than the given element in the argument of this method, or it will return null if there is no such element as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  System.out.println("highest value than 4 :  " + treeSetObject1.higher(4) );

  System.out.println("highest value than 5 :  " + treeSetObject1.higher(5) );

  }

}

Output:

highest value than 4 :  5

highest value than 5 :  null

15. boolean isEmpty():

This method returns true if the set is empty or false if set contains any element, as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

  TreeSet <Integer>treeSetObject1 = new TreeSet<Integer>();
  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  System.out.println("Is Tree Set empty ? " +treeSetObject1.isEmpty());

treeSetObject1.clear();

  System.out.println("Is Tree Set empty ? " +treeSetObject1.isEmpty());

  }

}

Output:

Is Tree Set empty ? false

Is Tree Set empty ? true

16. Iterator iterator():

This method gives an object of Iterator class by which we can retrieve list in increasing order as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  Iterator iteratorobj;

  iteratorobj = treeSetObject1.iterator();

  while (iteratorobj.hasNext())
  {

    System.out.println(iteratorobj.next() + " ");

  }

 }

}

Output:

1

2

3

4

5

17. Object last():

This method returns the highest element present in the treeset, as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  System.out.println("Last elememt in Tree Set object is: "+treeSetObject1.last());

  }

}

Output:

Last elememt in Tree Set object is: 5

18. Object lower(Object o):

This method returns the greatest element in this treeset which is strictly less than the given element in the set, or it will return null if there is no such element as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  System.out.println("Greatest Element Less than 5: "+treeSetObject1.lower(5));

  System.out.println("Greatest Element Less than 1: "+treeSetObject1.lower(1));

  }

}

Output:

Greatest Element Less than 5: 4

Greatest Element Less than 1: null

19. Object pollFirst():

This method returns as well as removes the lowest element, or it will returns null if this tree set is empty as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  System.out.println("Removed Element using Pollfirst method "+treeSetObject1.pollFirst());

  System.out.println("Values in Tree Set object: " + treeSetObject1);

  }

}

Output:

Removed Element using Pollfirst method 1

Values in Tree Set object: [2, 3, 4, 5]

20. Object pollLast():

This method retrieves as well as removes the highest element, or it will returns null if this set is empty as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  System.out.println("Removed Element using PollLast method "+treeSetObject1.pollLast());

  System.out.println("Values in Tree Set object: " + treeSetObject1);

  }

}

Output:

Removed Element using PollLast method 5

Values in Tree Set object: [1, 2, 3, 4]

21. boolean remove(Object o):

This method removes the element specified from the treeset, if it is present, as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  System.out.println("Is element '1' removed ? " +treeSetObject1.remove(1));

  System.out.println("Values in Tree Set Object: " +treeSetObject1);

  System.out.println("Is element '6' removed ? " +treeSetObject1.remove(6));

  }

}

Output:

Is element '1' removed ? true

Values in Tree Set Object: [2, 3, 4, 5]

Is element '6' removed ? false

22. Integer size():

This method gives the size of treeset, as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  System.out.println("Size of the Tree Set Object : " +treeSetObject1.size());

  }

}

Output:

Size of the Tree Set Object : 5

23. NavigableSet subSet(Object fromElement, boolean fromInclusive, Object toElement, boolean toInclusive):

This method returns group of elements that range from fromElement to toElement, Boolean keywords are added that tells whether “fromElement” or “toElement” should be added in group or not, as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

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

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

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

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  treeSetObject1.add(8);

  treeSetObject1.add(9);

  treeSetObject1.add(10);

  treeSetObject1.add(9);

  treeSetObject1.add(12);

  treeSetObject2=(TreeSet<Integer>) treeSetObject1.subSet(5, true, 10, true);

  treeSetObject3=(TreeSet<Integer>) treeSetObject1.subSet(5, false, 10, false);

  treeSetObject4=(TreeSet<Integer>) treeSetObject1.subSet(5, true, 10, false);

  treeSetObject5=(TreeSet<Integer>) treeSetObject1.subSet(5, false, 10, true);

  System.out.println("Values in tree Set Object 2 :" +treeSetObject2);

  System.out.println("Values in tree Set Object 3 :" +treeSetObject3);

  System.out.println("Values in tree Set Object 4 :" +treeSetObject4);

  System.out.println("Values in tree Set Object 5 :" +treeSetObject5);

  }

}

Output:

Values in tree Set Object 2 :[5, 8, 9, 10]

Values in tree Set Object 3 :[8, 9]

Values in tree Set Object 4 :[5, 8, 9]

Values in tree Set Object 5 :[8, 9, 10]

24. SortedSet subSet(Object fromElement, Object toElement):

This method gives group of elements that range from fromElement, inclusive, to toElement, exclusive. As shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  treeSetObject1.add(8);

  treeSetObject1.add(9);

  treeSetObject1.add(10);

  treeSetObject1.add(9);

  treeSetObject1.add(12);

  treeSetObject2=(TreeSet<Integer>) treeSetObject1.subSet(4, 10);

  System.out.println("Values in tree Set Object 2 :" +treeSetObject2);

  }

}

Output:

Values in tree Set Object 2 :[4, 5, 8, 9]

25. SortedSet tailSet(Object fromElement):

This method returns group of elements that are greater than or equal to fromElement as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  treeSetObject1.add(8);

  treeSetObject1.add(9);

  treeSetObject1.add(10);

  treeSetObject1.add(9);

  treeSetObject1.add(12);

  treeSetObject2=(TreeSet<Integer>) treeSetObject1.tailSet(8);

  System.out.println("Values in tree Set Object 2 :" +treeSetObject2);

  }

}

Output:

Values in tree Set Object 2 :[8, 9, 10, 12]

26. NavigableSet tailSet(Object fromElement, Boolean inclusive):

This method gives group of elements that are greater than (or equal to, if inclusive is true) fromElement as shown in the following program:

import java.util.*;

public class TreSetMethodsDemo
{

  public static void main(String args[]){

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

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

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

  treeSetObject1.add(3);

  treeSetObject1.add(5);

  treeSetObject1.add(4);

  treeSetObject1.add(1);

  treeSetObject1.add(2);

  treeSetObject1.add(8);

  treeSetObject1.add(9);

  treeSetObject1.add(10);

  treeSetObject1.add(9);

  treeSetObject1.add(12);

  treeSetObject2=(TreeSet<Integer>) treeSetObject1.tailSet(8,true);

  treeSetObject3=(TreeSet<Integer>) treeSetObject1.tailSet(8,false);

  System.out.println("Values in tree Set Object 2 :" +treeSetObject2);

  System.out.println("Values in tree Set Object 3 :" +treeSetObject3);

  }

}

Output:

Values in tree Set Object 2 :[8, 9, 10, 12]

Values in tree Set Object 3 :[9, 10, 12]