TreeSet in Java

HashSet

In the previous blog, we learned about HashSet in Java. If you want to know more about it, visit HashSet in Java. In this blog, we will learn about another Class in Collection Framework i.e. TreeSet. TreeSet is similar to HashSet, but the difference is, it is a sorted Collection. i.e. The elements inserted in random order will be presented in sorted order. For example, suppose we insert 4 elements in random order and we visit the elements. Then the values of those elements will be displayed in sorted order. Let's go ahead and take a brief look into it.


TreeSet

As the name suggests the sorting in the TreeSet is achieved by a tree data structure. Elements are stored in sorted order. TreeSet inherits AbstractSet class and implements the NavigableSet interface.

...

Syntax of Creating a TreeSet

Before creating a TreeSet, it is mandatory to import the package named java.util.TreeSet. If the package will not be imported then the compiler will give an error. So it is must to import the above package. Once the package is imported, we can create a TreeSet.

Syntax

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

Methods of TreeSet

  1. add()
  2. add() method adds the specified element to the set if it is not present already.

    e.g.

    import java.util.TreeSet;
    class AddMethodEx {
      public static void main(String[] args) {
        TreeSet<String> str = new TreeSet<>();
        str.add("John");
        str.add("Smith");
        str.add("Jackson");
        str.add("Elfred");
        System.out.println("The elements in TreeSet are: " + str);
      }
    }

    Output

    The elements in TreeSet are: [Elfred, Jackson, John, Smith]
  3. addAll()
  4. addAll() method is used to all elements of the specified Collection to the set.

    e.g.

    import java.util.TreeSet;
    class AddAllMethodEx {
      public static void main(String[] args) {
        TreeSet<String> str = new TreeSet<>();
        str.add("John");
        str.add("Smith");
        str.add("Jackson");
        str.add("Elfred");
        System.out.println("The elements in TreeSet are: " + str);
        TreeSet<String> str1 = new TreeSet<>();
        str1.add("Leo");
        str1.addAll(str);
        System.out.println("After add the new element in TreeSet are: " + str);
      }
    }

    Output

    The elements in TreeSet are: [Elfred, Jackson, John, Smith]
    After add the new element in TreeSet are: [Elfred, Jackson, John, Smith]
  5. remove()
  6. remove() method is to remove or delete the specified element from the set if it is present.

    e.g.

    import java.util.TreeSet;
    class RemoveMethodEx {
      public static void main(String[] args) {
        TreeSet<String> str = new TreeSet<>();
        str.add("John");
        str.add("Smith");
        str.add("Jackson");
        str.add("Elfred");
        System.out.println("The elements in TreeSet are: " + str);
        boolean element = str.remove("Jackson");
        System.out.println("Is the string deleted: " + element);
        System.out.println("After removing the element from the TreeSet: " + str);
      }
    }

    Output

    The elements in TreeSet are: [Elfred, Jackson, John, Smith]
    Is the string deleted: true
    After removing the element from the TreeSet: [Elfred, John, Smith]
  7. removeAll()
  8. removeAll() method is used to remove or delete all the elements from the set.

    e.g.

    import java.util.TreeSet;
    class RemoveAllMethodEx {
      public static void main(String[] args) {
        TreeSet<String> str = new TreeSet<>();
        str.add("John");
        str.add("Smith");
        str.add("Jackson");
        str.add("Elfred");
        System.out.println("The elements in TreeSet are: " + str);
        boolean element = str.removeAll(str);
        System.out.println("Are all elements deleted from the TreeSet: " + element);
        System.out.println("After removing all the elements from the TreeSet: " + str);
      }
    }

    Output

    The elements in TreeSet are: [Elfred, Jackson, John, Smith]
    Are all elements deleted from the TreeSet: true
    After removing all the elements from the TreeSet: [ ]
  9. iterator()
  10. iterator() method is used to access the elements of the TreeSet. But in order to use the iterator() we must import a package named java.util.Iterator.

    e.g.

    import java.util.TreeSet;
    import java.util.Iterator;
    class IteratorMethodEx {
      public static void main(String[] args) {
        TreeSet<String> str = new TreeSet<>();
        str.add("John");
        str.add("Smith");
        str.add("Jackson");
        str.add("Elfred");
        System.out.println("The elements in TreeSet are: " + str);
        Iterator<String> itr = str.iterator();
        System.out.print("Displaying the elements of the TreeSet using Iterator: ");
        while(itr.hasNext()) {
            System.out.print(itr.next());
            System.out.print(", ");
        }
      }
    }

    Output

    The elements in TreeSet are: [Elfred, Jackson, John, Smith]
    Displaying the elements of the TreeSet using Iterator: Elfred, Jackson, John, Smith,
  11. first()
  12. first() method returns the first element of the set.

    e.g.

    import java.util.TreeSet;
    class FirstMethodEx {
      public static void main(String[] args) {
        TreeSet<String> str = new TreeSet<>();
        str.add("John");
        str.add("Smith");
        str.add("Jackson");
        str.add("Elfred");
        System.out.println("The elements in TreeSet are: " + str);
        System.out.println("The first element of the TreeSet : " + str.first());
      }
    }

    Output

    The elements in TreeSet are: [Elfred, Jackson, John, Smith]
    The first element of the TreeSet : Elfred
  13. last()
  14. last() method returns the last element of the set.

    e.g.

    import java.util.TreeSet;
    class LastMethodEx {
      public static void main(String[] args) {
        TreeSet<String> str = new TreeSet<>();
        str.add("John");
        str.add("Smith");
        str.add("Jackson");
        str.add("Elfred");
        System.out.println("The elements in TreeSet are: " + str);
        System.out.println("The last element of the TreeSet : " + str.last());
      }
    }

    Output

    The elements in TreeSet are: [Elfred, Jackson, John, Smith]
    The last element of the TreeSet : Smith
  15. higher()
  16. higher() method returns the least element among those elements that are greater than the specified element.

    e.g.

    import java.util.TreeSet;
    class HigherMethodEx {
      public static void main(String[] args) {
        TreeSet<Integer> nos = new TreeSet<>();
        nos.add(10);
        nos.add(9);
        nos.add(80);
        nos.add(4);
        System.out.println("The elements in TreeSet are: " + nos);
        System.out.println("Element displayed using higher() : " + nos.higher(4));
      }
    }

    Output

    The elements in TreeSet are: [4, 9, 10, 80]
    Element displayed using higher() : 9
  17. lower()
  18. lower() method returns the greatest element among those elements that are less than the specified element.

    e.g.

    import java.util.TreeSet;
    class LowerMethodEx {
      public static void main(String[] args) {
        TreeSet<Integer> nos = new TreeSet<>();
        nos.add(10);
        nos.add(9);
        nos.add(80);
        nos.add(4);
        System.out.println("The elements in TreeSet are: " + nos);
        System.out.println("Element displayed using lower() : " + nos.lower(50));
      }
    }

    Output

    The elements in TreeSet are: [4, 9, 10, 80]
    Element displayed using lower() : 10
  19. ceiling()
  20. The ceiling() method returns the smallest element in this set that is bigger than or equal to the provided element, or null if no such element exists.

    e.g.

    import java.util.TreeSet;
    class CeilingMethodEx {
      public static void main(String[] args) {
        TreeSet<Integer> nos = new TreeSet<>();
        nos.add(10);
        nos.add(9);
        nos.add(80);
        nos.add(4);
        System.out.println("The elements in TreeSet are: " + nos);
        System.out.println("Element displayed using ceiling() : " + nos.ceiling(50));
      }
    }

    Output

    The elements in TreeSet are: [4, 9, 10, 80]
    Element displayed using ceiling() : 80
  21. floor()
  22. The floor() method returns the equal or nearest least element in the set to the provided element, or null if no such element exists.

    e.g.

    import java.util.TreeSet;
    class FloorMethodEx {
      public static void main(String[] args) {
        TreeSet<Integer> nos = new TreeSet<>();
        nos.add(10);
        nos.add(9);
        nos.add(80);
        nos.add(4);
        System.out.println("The elements in TreeSet are: " + nos);
        System.out.println("Element displayed using floor() : " + nos.floor(90));
      }
    }

    Output

    The elements in TreeSet are: [4, 9, 10, 80]
    Element displayed using floor() : 80
  23. pollFirst()
  24. pollFirst() method returns and removes the first element from the set.

    e.g.

    import java.util.TreeSet;
    class PollFirstMethodEx {
      public static void main(String[] args) {
        TreeSet<Integer> nos = new TreeSet<>();
        nos.add(10);
        nos.add(9);
        nos.add(80);
        nos.add(4);
        System.out.println("The elements in TreeSet are: " + nos);
        System.out.println("Deleting the first element: " + nos.pollFirst());
        System.out.println("The new TreeSet after deleting the first element: " + nos);
      }
    }

    Output

    The elements in TreeSet are: [4, 9, 10, 80]
    Deleting the first element: 4
    The new TreeSet after deleting the first element: [9, 10, 80]
  25. pollLast()
  26. pollLast() method returns and removes the last element from the set.

    e.g.

    import java.util.TreeSet;
    class PollLastMethodEx {
    	public static void main(String[] args) {
       	 TreeSet<Integer> nos = new TreeSet<>();
       	 nos.add(10);
       	 nos.add(9);
       	 nos.add(80);
       	 nos.add(4);
       	 System.out.println("The elements in TreeSet are: " + nos);
       	 System.out.println("Deleting the last element: " + nos.pollLast());
       	 System.out.println("The new TreeSet after deleting the last element: " + nos);
      }
    }

    Output

    The elements in TreeSet are: [4, 9, 10, 80]
    Deleting the last element: 80
    The new TreeSet after deleting the last element: [4, 9, 10]
  27. headSet()
  28. headSet() method returns all the elements of a TreeSet before the specified element. If the value true is passed to the method, then the method returns all the elements before the specified element including the specified element. The default value is false.

    e.g.

    import java.util.TreeSet;
    class HeadSetMethodEx {
      public static void main(String[] args) {
        TreeSet<Integer> nos = new TreeSet<>();
        nos.add(10);
        nos.add(9);
        nos.add(80);
        nos.add(4);
        System.out.println("The elements in TreeSet are: " + nos);
        System.out.println("Displaying elements using headset() method without boolean value: " + nos.headSet(80));
        System.out.println("Displaying elements using headset() method with boolean value: " + nos.headSet(80, true));
      }
    }

    Output

    The elements in TreeSet are: [4, 9, 10, 80]
    Displaying elements using headSet() method without boolean value: [4, 9, 10]
    Displaying elements using headSet() method with boolean value: [4, 9, 10, 80]
  29. tailSet()
  30. tailSet() method returns all the elements of a TreeSet after the specified element. If the value false is passed to the method, then the method returns all the elements after the specified element including the specified element. The default value is true.

    e.g.

    import java.util.TreeSet;
    class TailSetMethodEx {
      public static void main(String[] args) {
        TreeSet<Integer> nos = new TreeSet<>();
        nos.add(10);
        nos.add(9);
        nos.add(80);
        nos.add(4);
        System.out.println("The elements in TreeSet are: " + nos);
        System.out.println("Displaying elements using tailSet() method without boolean value: " + nos.tailSet(9));
        System.out.println("Displaying elements using tailSet() method with boolean value: " + nos.headSet(9, false));
      }
    }

    Output

    The elements in TreeSet are: [4, 9, 10, 80]
    Displaying elements using tailSet() method without boolean value: [9, 10, 80]
    Displaying elements using tailSet() method with boolean value: [10, 80]
Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.