TreeSet in java is used to store objects in sorted order. By default natural sorting order is followed. Be can use the Comparator class to create user-defined sorting order.

TreeSet in java implements the NavigableSet interface. NavigableSet interface extends the SortedSet interface and SortedSet extends the Set interface. Hence TreeSet in java indirectly implements the Set interface as shown below. 

Following are the constructors for TreeSet in Java:

  • public TreeSet(): Creates a new, empty tree set, with the natural ordering of its elements.
  • public TreeSet(Collection<? extends E> c): Creates a new tree set containing the elements in the specified collection. Elements are sorted according to natural sorting order.
  • public TreeSet(Comparator<? super E> comparator): Creates a new empty tree set. User-defined comparator class is used for sorting the elements.
  • public TreeSet(SortedSet<E> s): Creates a new tree set containing the same elements and using the same ordering as the specified sorted set.

Example for TreeSet in java using the above constructors.

Java Code
import java.util.*;
public class Test {
public static void main(String[] args) {
// 1. By Using TreeSet()
TreeSet<Integer> set1 = new TreeSet<>();
set1.add(45);
set1.add(12);
set1.add(67);
System.out.println(“set1 is :” + set1);

// 2. By using TreeSet(Collection<? extends E> c)
List<Integer> elements = Arrays.asList(32,12,45,2,56,32,1);
TreeSet<Integer> set2 = new TreeSet<>(elements);
System.out.println(“set2 is :” + set2);

// 3. By using TreeSet(Comparator<? super E> comparator)
TreeSet<Integer> set3 = new TreeSet<Integer>(new MyComparator());
set3.add(45);
set3.add(12);
set3.add(67);
System.out.println(“set3 is :” + set3);

// 4. By using public TreeSet(SortedSet s). Note that TreeSet is implementation class for SortedSet hence passing TreeSet object
TreeSet<Integer> set4 = new TreeSet<>(set3);
set4.add(33);
set4.add(24);
System.out.println(“set4 is :” + set4);
}
}

// Comparator for reverse sorting order
class MyComparator implements Comparator<Object>{
public int compare(Object o1, Object o2) {
Integer i1 = (Integer) o1;
Integer i2 = (Integer) o2;
if(i1 > i2)
return -1;
else if(i1 < i2)
return 1;
else
return 0;
}
}
Output:
set1 is :[12, 45, 67]
set2 is :[1, 2, 12, 32, 45, 56]
set3 is :[67, 45, 12]
set4 is :[67, 45, 33, 24, 12]

Following are the methods for TreeSet in Java:

  1. boolean add(E e): Adds the element if it is not already present.
  2. boolean addAll(Collection<? extends E> c): Adds all of the elements.
  3. void clear(): Removes all of the elements from this set.
  4. Object clone(): Returns a shallow copy of this TreeSet instance.
  5. Comparator<? super E> comparator(): Returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.
  6. boolean contains(Object o): Returns true if this set contains the specified element.
  7. boolean isEmpty(): Returns true if this set contains no elements.
  8. int size(): Returns the number of elements in this set.

Following is an example of the above methods:

Java Code
import java.util.*;
public class Test {
public static void main(String[] args) {
List<Integer> elements = Arrays.asList(32,12,45,2,56,32,1);
TreeSet<Integer> set1 = new TreeSet<Integer>(new MyComparator());
set1.add(45); // 1. boolean add(E e)
set1.addAll(elements); // 2. boolean addAll(Collection<? extends E> c)
System.out.println(“set1 is : ” + set1);

TreeSet<Integer> set2 =(TreeSet<Integer>) set1.clone(); // 4. Object clone()
System.out.println(“set2 is : ” + set2);

set2.clear(); // 3. void clear()
System.out.println(“set2 after clear is :” + set2);
System.out.println(“Comarator : ” + set1.comparator()); // 5. Comparator<? super E> comparator()

System.out.println(“set1 contains(45): ” + set1.contains(45)); // 6. boolean contains(Object o)

// 7. boolean isEmpty()
System.out.println(“set1 isEmpty: ” + set1.isEmpty() + “, set2 isEmpty: ” + set2.isEmpty());

// 8. int size()
System.out.println(“set1 size: ” + set1.size() + “, set2 size: ” + set2.size());

}
}

// Comparator for reverse sorting order
class MyComparator implements Comparator<Object>{
public int compare(Object o1, Object o2) {
Integer i1 = (Integer) o1;
Integer i2 = (Integer) o2;
if(i1 > i2)
return -1;
else if(i1 < i2)
return 1;
else
return 0;
}
}
Output:
set1 is : [56, 45, 32, 12, 2, 1] set2 is : [56, 45, 32, 12, 2, 1] set2 after clear is :[] Comarator : MyComparator@7637f22
set1 contains(45): true
set1 isEmpty: false, set2 isEmpty: true
set1 size: 6, set2 size: 0

  1. Iterator<E> iterator(): Returns an iterator over the elements in this set in ascending order.
  2. Iterator<E> descendingIterator(): Returns an iterator over the elements in this set in descending order.
  3. NavigableSet<E> descendingSet(): Returns a reverse order view of the elements contained in this set.

Following is an example of the above methods:

Java Code
import java.util.*;
public class Test {
public static void main(String[] args) {
List<Integer> elements = Arrays.asList(32,12,45,2,56,32,1);
TreeSet<Integer> set1 = new TreeSet<Integer>(elements);

System.out.println(“set1 is : ” + set1);

// 9. Iterator iterator()
Iterator<Integer> itr1 = set1.iterator();
System.out.print(“Value for itr1: “);
while(itr1.hasNext()){
System.out.print(” ” + itr1.next());
}
System.out.println();

// 10. Iterator descendingIterator()
Iterator<Integer> itr2 = set1.descendingIterator();
System.out.print(“Value for itr2: “);
while(itr2.hasNext()){
System.out.print(” ” + itr2.next());
}
System.out.println();

// 11. NavigableSet descendingSet()
NavigableSet<Integer> nSet = set1.descendingSet();
System.out.println(“NavigableSet is : ” + nSet);
}
}
Output:
set1 is : [1, 2, 12, 32, 45, 56]
Value for itr1:1 2 12 32 45 56
Value for itr2:56 45 32 12 2 1
NavigableSet is : [56, 45, 32, 12, 2, 1]

  1. E first(): Returns the first (lowest) element currently in this set.
  2. E last(): Returns the last (highest) element currently in this set.
  3. E ceiling(E e): To return the least element in this set greater than or equal to the given element, or null if there is no such element.
  4. E floor(E e): Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.
  5. E higher(E e): to return the least element in this set strictly greater than the given element, or null if there is no such element.
  6. E lower(E e): Returns the greatest element in this set strictly less than the given element, or null if there is no such element.

Following is an example of the above methods:

Java Code
import java.util.*;
public class Test {
public static void main(String[] args) {
List<Integer> elements = Arrays.asList(32,12,45,2,56,32,1);
TreeSet<Integer> set1 = new TreeSet<Integer>(elements);
System.out.println(“set1 is : ” + set1);

// 12. E first()
System.out.println(“set1.first(): ” + set1.first());

// 13. E last()
System.out.println(“set1.last(): ” + set1.last());

// 14. E ceiling(E e)
System.out.println(“set1.ceiling(30): ” + set1.ceiling(30));
System.out.println(“set1.ceiling(32): ” + set1.ceiling(32));

// 15. E floor(E e)
System.out.println(“set1.floor(30): ” + set1.floor(30));
System.out.println(“set1.floor(32): ” + set1.floor(32));

// 16. E higher(E e)
System.out.println(“set1.higher(30): ” + set1.higher(30));
System.out.println(“set1.higher(32): ” + set1.higher(32));

// 17. E lower(E e)
System.out.println(“set1.lower(30): ” + set1.lower(30));
System.out.println(“set1.lower(32): ” + set1.lower(32));
}
}
Output:
set1 is : [1, 2, 12, 32, 45, 56]
set1.first(): 1
set1.last(): 56
set1.ceiling(30): 32
set1.ceiling(32): 32
set1.floor(30): 12
set1.floor(32): 32
set1.higher(30): 32
set1.higher(32): 45
set1.lower(30): 12
set1.lower(32): 12

  1. E pollFirst(): Retrieves and removes the first (lowest) element, or returns null if this set is empty.
  2. E pollLast(): Retrieves and removes the last (highest) element, or returns null if this set is empty.
  3. boolean remove(Object o): Removes the specified element from this set if it is present.

Following is an example of the above methods:

Java Code
import java.util.*;
public class Test {
public static void main(String[] args) {
List<Integer> elements = Arrays.asList(32,12,45,2,56,32,1);
TreeSet<Integer> set1 = new TreeSet<Integer>(elements);
System.out.println(“set1 is : ” + set1);

// 18. E pollFirst()
System.out.println(“set1.pollFirst(): ” + set1.pollFirst());
System.out.println(“set1 is : ” + set1);

// 19. E pollLast()
System.out.println(“set1.pollLast(): ” + set1.pollLast());
System.out.println(“set1 is : ” + set1);

// 20. boolean remove(Object o)
System.out.println(“set1.remove(12): ” + set1.remove(12));
System.out.println(“final set1 is : ” + set1);
}
}
Output:
set1 is : [1, 2, 12, 32, 45, 56]
set1.pollFirst(): 1
set1 is : [2, 12, 32, 45, 56]
set1.pollLast(): 56
set1 is : [2, 12, 32, 45]
set1.remove(12): true
final set1 is : [2, 32, 45]

  1. SortedSet<E> headSet(E toElement): Returns a view of the portion of this set whose elements are strictly less than toElement.
  2. NavigableSet<E> headSet(E toElement, boolean inclusive): Returns a view of the portion of this set whose elements are less than (or equal to, if inclusive is true) toElement.
  3. NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive): Returns a view of the portion of this set whose elements range from fromElement to toElement.
  4. SortedSet<E> subSet(E fromElement, E toElement): Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.
  5. SortedSet<E> tailSet(E fromElement): Returns a view of the portion of this set whose elements are greater than or equal to fromElement.
  6. NavigableSet<E> tailSet(E fromElement, boolean inclusive): Returns a view of the portion of this set whose elements are greater than (or equal to, if inclusive is true) fromElement.

Following is an example of the above methods:

Java Code
import java.util.*;
public class Test {
public static void main(String[] args) {
List<Integer> elements = Arrays.asList(32,12,45,2,56,32,1);
TreeSet<Integer> set1 = new TreeSet<Integer>(elements);
System.out.println(“set1 is : ” + set1);

// 21. SortedSet headSet(E toElement)
System.out.println(“set1.headSet(30): ” + set1.headSet(30));
System.out.println(“set1.headSet(32): ” + set1.headSet(32));

// 22. NavigableSet headSet(E toElement, boolean inclusive)
System.out.println(“set1.headSet(32, false): ” + set1.headSet(32, false));
System.out.println(“set1.headSet(32, true): ” + set1.headSet(32, true));

// 23. NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
System.out.println(“set1.subSet(1, false, 32, false): ” + set1.subSet(1, false, 32, false));
System.out.println(“set1.subSet(1, true, 32, true): ” + set1.subSet(1, true, 32, true));

// 24. SortedSet subSet(E fromElement, E toElement)
System.out.println(“set1.subSet(10, 56): ” + set1.subSet(10, 56));
System.out.println(“set1.subSet(10, 60): ” + set1.subSet(10, 60));

// 25. SortedSet tailSet(E fromElement)
System.out.println(“set1.tailSet(10): ” + set1.tailSet(10));
System.out.println(“set1.tailSet(12): ” + set1.tailSet(12));

// 26. NavigableSet tailSet(E fromElement, boolean inclusive)
System.out.println(“set1.tailSet(12, false): ” + set1.tailSet(12, false));
System.out.println(“set1.tailSet(12, true): ” + set1.tailSet(12, true));
}
}
Output:
set1 is : [1, 2, 12, 32, 45, 56]
set1.headSet(30): [1, 2, 12]
set1.headSet(32): [1, 2, 12]
set1.headSet(32, false): [1, 2, 12]
set1.headSet(32, true): [1, 2, 12, 32]
set1.subSet(1, false, 32, false): [2, 12]
set1.subSet(1, true, 32, true): [1, 2, 12, 32]
set1.subSet(10, 56): [12, 32, 45]
set1.subSet(10, 60): [12, 32, 45, 56]
set1.tailSet(10): [12, 32, 45, 56]
set1.tailSet(12): [12, 32, 45, 56]
set1.tailSet(12, false): [32, 45, 56]
set1.tailSet(12, true): [12, 32, 45, 56]

-A blog by Shwetali Khambe

Related Posts