Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does EnumSet have many overloaded "of" methods?

While going through the EnumSet<E> of method, I have seen multiple overloaded implementations of of method:

public static <E extends Enum<E>> EnumSet<E> of(E e)  public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2)  . .  public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4, E e5) 

and then another overloaded method with varargs

public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest) {     EnumSet<E> result = noneOf(first.getDeclaringClass());     result.add(first);     for (E e : rest)         result.add(e);     return result; } 

When this varargs could have handled the other implementations, why this method is overloaded this way? Is there any specific reason for this?

I had gone through the Javadoc of the same, but I could not find any convincing explanation.

like image 283
Somnath Musib Avatar asked Aug 10 '15 16:08

Somnath Musib


People also ask

Is EnumSet immutable?

Yes EnumSet is modifiable.

What is the use of EnumSet in Java?

EnumSet class is a member of the Java Collections Framework & is not synchronized. It's a high-performance set implementation, much faster than HashSet. 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.

What is enum collection?

An EnumSet is a specialized Set collection to work with enum classes. It implements the Set interface and extends from AbstractSet: Even though AbstractSet and AbstractCollection provide implementations for almost all the methods of the Set and Collection interfaces, EnumSet overrides most of them.

What are the methods in EnumSet?

Methods in EnumSet METHOD DESCRIPTION allOf​ (Class<E> elementType) Creates an enum set containing all of th ... clone () Returns a copy of this set. complementOf​ (EnumSet<E> s) Creates an enum set with the same elemen ... copyOf​ (Collection<E> c) Creates an enum set initialized from the ... 9 more rows ...

What is EnumSet of in Java?

EnumSet of() Method in Java. The java.util.EnumSet.of(E ele1, E ele2, E ele3, …) method in Java is used to create an enum set initially containing the specified elements in the parameters. When multiple items are added at the same time the elements are pushed down the set as the new elements are added.

Why is EnumSet faster than HashSet in Java?

Due to its implementation using RegularEnumSet and JumboEnumSet, all the methods in an EnumSet are implemented using bitwise arithmetic operations. EnumSet is faster than HashSet because we no need to compute any hashCode to find the right bucket.

How to create enum using static methods in Java?

static <E extends Enum<E>> of (E e1, E e2, E e3, E e4, E e5): This method is also create the enum with specifies elements passed as enum. Return type is EnumSet<E> static <E extends Enum<E>> range (E from, E to): In this method we can specified the range. Then it will create a new enum. Return type is EnumSet<E> .


1 Answers

Varargs methods create an array.

public static void foo(Object... args) {   System.out.println(args.length); } 

This works, because of the implicit array creation. EnumSet is a class designed to be very, very fast, so by creating all the extra overloads they can skip the array creation step in the first few cases. This is especially true since in many cases Enum don't have that many elements, and if they do, the EnumSet might not contain all of them.

Javadoc for EnumSet<E> of(E e1, E e2, E e3, E e4, E e5):

Creates an enum set initially containing the specified elements. Overloadings of this method exist to initialize an enum set with one through five elements. A sixth overloading is provided that uses the varargs feature. This overloading may be used to create an enum set initially containing an arbitrary number of elements, but is likely to run slower than the overloadings that do not use varargs.

like image 136
durron597 Avatar answered Sep 20 '22 19:09

durron597