Difference Between EnumSet and TreeSet in Java Last Updated : 28 Jun, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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.Example: Java // Java program to demonstrate // the EnumSet import java.util.*; class enumSetExample { enum Colors { Red, Pink, Grey, Yellow, Green } 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()) { System.out.println(itr.next()); } } } OutputEnumSet : Pink Green 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. Example: Java // 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 ts.add("Geek"); ts.add("For"); ts.add("Geeks"); ts.add("welcomes"); ts.add("you"); 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)); } } OutputTree 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 TreeSetBasicEnumSet 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. Comment More infoAdvertise with us Next Article Difference Between TreeSet and SortedSet in Java P prashant_srivastava Follow Improve Article Tags : Java Technical Scripter 2020 Java-Collections java-treeset java-EnumSet +1 More Practice Tags : JavaJava-Collections Similar Reads Difference between TreeMap and TreeSet in Java TreeSet is mainly an implementation of SortedSet in java where duplication is not allowed and objects are stored in sorted and ascending order. Some important features of the TreeSet are: In TreeSet duplicate values are not allowed because it implements the SortedSet interface.Objects in a TreeSet a 2 min read Difference Between EnumMap and EnumSet in Java EnumMap and EnumSet both are the classes defined inside the java collection. In this article, we will learn the differences between EnumMap and EnumSet. EnumMap is the specialized implementation of the Map interface and the EnumSet is the specialized implementation of the Set interface. There are so 3 min read Difference Between List and Set in Java The List interface allows storing the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values are allowed to store. List preserves the insertion order, it allows positional access and insertion of elements. Declaration: public abstr 2 min read Difference Between TreeSet and SortedSet in Java TreeSet is one of the implementations of the Navigable sub-interface. It is underlying data structure is a red-black tree. The elements are stored in ascending order and more methods are available in TreeSet compare to SortedSet. We can also change the sorting parameter using a Comparator. For examp 3 min read Difference between HashMap and HashSet HashSet is an implementation of Set Interface which does not allow duplicate value. The main thing is, objects that are stored in HashSet must override equals() for check for equality, and hashCode() methods for no duplicate value are stored in our set. HashMap is an implementation of Map Interface, 4 min read Difference between HashMap and HashSet HashSet is an implementation of Set Interface which does not allow duplicate value. The main thing is, objects that are stored in HashSet must override equals() for check for equality, and hashCode() methods for no duplicate value are stored in our set. HashMap is an implementation of Map Interface, 4 min read Like