Understand enummap and enumset

Understand enummap and enumset

brief introduction

Generally speaking, we will choose to use HashMap to store data in key value format. Considering such a special case, the keys of a HashMap come from an enum class. In this case, we can consider using enummap to be discussed in this article.

EnumMap

Let's take a look at the comparison between enummap definition and HashMap definition:

public class EnumMap<K extends Enum<K>,V> extends AbstractMap<K,V>
    implements java.io.Serializable,Cloneable
public class HashMap<K,V>
    implements Map<K,V>,Cloneable,Serializable 

We can see that enummap is almost the same as HashMap. The difference is that the key of enummap is an enum.

Here is a simple example:

Define an enum first:

public enum Types {
    RED,GREEN,BLACK,YELLO
}

Let's see how to use enummap:

    @Test
    public void useEnumMap(){
        EnumMap<Types,String> activityMap = new EnumMap<>(Types.class);
        activityMap.put(Types.BLACK,"black");
        activityMap.put(Types.GREEN,"green");
        activityMap.put(Types.RED,"red");
    }

Other operations are actually similar to HashMap. We won't talk about them here.

When to use enummap

Because the possible values of all keys in enummap are known at the time of creation, using enummap can improve efficiency compared with HashMap.

At the same time, because the key is relatively simple, there is no need to consider some complex situations like HashMap in the implementation of enummap.

EnumSet

Similar to enummap, enumset is a set, and the elements in the set are of an enum type.

Enumset is an interface rather than a class. To create an enumset class, you can use two static methods provided by enumset, noneof and allof.

Let's look at a noneof:

    public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) {
        Enum<?>[] universe = getUniverse(elementType);
        if (universe == null)
            throw new ClassCastException(elementType + " not an enum");

        if (universe.length <= 64)
            return new RegularEnumSet<>(elementType,universe);
        else
            return new JumboEnumSet<>(elementType,universe);
    }

Noneof passes in an enum class and returns an empty enumset of enum type.

From the above code, we can see that enumset has two implementations. Jumboenumset is used when the length is greater than 64 and regularenumset is used when the length is less than 64.

Note that jumboenumset and regularenumset are not recommended to be used directly. They are internal classes.

Take another look at allof:

public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType) {
        EnumSet<E> result = noneOf(elementType);
        result.addAll();
        return result;
    }

AllOf is very simple. First call noneOf to create an empty set, then call the addAll method to add all the elements.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>