Skip to content
Related Articles

Related Articles

Difference Between EnumSet and TreeSet in Java
  • Last Updated : 22 Jan, 2021

EnumSet and TreeSet both are the classes defined inside the collection framework. But there are few differences exists between them. In this article, we have tried to cover all these differences between them.

1. EnumSet: EnumSet is a specialized implementation of the Set interface for enumeration types. It extends AbstractSet and implements the Set interface in Java. Few important points of EnumSet are as follows: 

  • EnumSet class is a member of the Java Collections Framework and it is not synchronized.
  • All of the elements in an EnumSet must come from a single enumeration type that is specified when the set is created either explicitly or implicitly.
  • EnumSet is much faster than HashSet.
  • EnumSet doesn’t allow to insert null object if we try to insert the null object, it will throw NullPointerException.
  • It uses a fail-safe iterator, so it won’t throw ConcurrentModificationException if the collection is modified while iterating.



// Java program to demonstrate
// the EnumSet
import java.util.*;
class enumSetExample {
    enum Colors {
    public static void main(String args[])
        // Creating an EnumSet
        EnumSet<Colors> colors
            = EnumSet.of(Colors.Pink, Colors.Green);
        Iterator<Colors> itr = colors.iterator();
        // Iterate and print elements to
        // the console
        System.out.println("EnumSet : ");
        while (itr.hasNext()) {
EnumSet : 

2. TreeSet: TreeSet is a class that implementation the SortedSet interface in Java. It uses the Tree for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. It can also be ordered by a Comparator provided at set creation time, depending on which constructor is used. The TreeSet implements a NavigableSet interface by inheriting AbstractSet class. The class which implements the navigable set is a TreeSet which is an implementation of a self-balancing tree. Therefore, this interface provides us with a way to navigate through this tree.



// Java code to demonstrate 
// the working of TreeSet 
import java.util.*; 
class TreeSetDemo { 
    public static void main(String[] args) 
        // Creating an empty TreeSet 
        TreeSet<String> ts = new TreeSet<String>(); 
        // Elements are added using add() method 
        System.out.println("Tree Set is " + ts); 
        String check = "welcomes"
        // Check if the above string exists in 
        // the treeset or not 
        System.out.println("Contains : " + check + " "
                        + ts.contains(check)); 
        // Print the first element in 
        // the TreeSet 
        System.out.println("First Value " + ts.first()); 
        // Print the last element in 
        // the TreeSet 
        System.out.println("Last Value " + ts.last()); 
        String value = "Geek"
        // Find the values just greater 
        // and smaller than the above string 
        System.out.println("Higher " + ts.higher(value)); 
        System.out.println("Lower " + ts.lower(value)); 
Tree Set is [For, Geek, Geeks, welcomes, you]
Contains : welcomes true
First Value For
Last Value you
Higher Geeks
Lower For

Difference between EnumSet and TreeSet:

PROPERTIES                        EnumSet                          TreeSet
BasicEnumSet is a specialized implementation of the Set interface.TreeSet is a class that implementation the SortedSet interface in java.
Data StructureIt internally represented as a BitVector.It internally represented as a Red-black tree.
SortingIt sorts the elements according to natural order.It sorts the elements according to sorted order.
IteratorEnumSet iterator is weakly consistent.TreeSet iterator is Fail-fast.
Best ChoiceEnumSet is best choice for storing enumeration type elements.TreeSet serves as an excellent choice for storing large amounts of sorted information which are supposed to be accessed quickly because of its faster access and retrieval time.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :