Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of EnumSet Abstract Class in java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "sample analysis of EnumSet abstract classes in java", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and study the "sample analysis of EnumSet abstract classes in java".

EnumSet

EnumSet is a generic container for Java enumerated types. Since Java has containers such as SortedSet, TreeSet, HashSet, and so on, why do you need an extra EnumSet? The answer must be that EnumSet has certain characteristics. For example, EnumSet is very fast. Other features are not listed, after all, the content of this article is not to introduce the features of EnumSet.

Collection classes designed specifically for enumerated classes, all elements must be enumerated types

The collection elements of EnumSet are ordered and stored internally in the form of bit vectors, so they take up less memory and are efficient.

Null elements are not allowed

Source code

Package java.util;import sun.misc.SharedSecrets;public abstract class EnumSet extends AbstractSet implements Cloneable, java.io.Serializable {/ * element type * / final ClasselementType; / * store elements through array * / final Enum [] universe; private static Enum [] ZERO_LENGTH_ENUM_ARRAY = new Enum [0]; EnumSet (ClasselementType, Enum [] universe) {this.elementType = elementType; this.universe = universe } / * create an empty enum set and define its element type * @ param elementType the class object of the element type for this enum * set * @ throws NullPointerException if elementType is null * / public static EnumSet noneOf (Class elementType) {Enum [] universe = getUniverse (elementType); if (universe = = null) throw new ClassCastException (elementType + "not an enum") If (universe.length 0} * @ return an enum set initially containing all of the elements in the * range defined by the two specified endpoints * / public static EnumSet range (E from, E to) {if (from.compareTo (to) > 0) throw new IllegalArgumentException (from + ">" + to); EnumSet result = noneOf (from.getDeclaringClass ()); result.addRange (from, to); return result } / * * Adds the specified range to this enum set, which is empty prior * to the call. * / abstract void addRange (E from, E to); / * * Returns a copy of this set. * * @ return a copy of this set * / public EnumSet clone () {try {return (EnumSet) super.clone ();} catch (CloneNotSupportedException e) {throw new AssertionError (e);}} / * * Complements the contents of this enum set. * / abstract void complement (); / * * Throws an exception if e is not of the correct type for this enum set. * / final void typeCheck (E e) {Class eClass = e.getClass (); if (eClass! = elementType & & eClass.getSuperclass ()! = elementType) throw new ClassCastException (eClass + "! =" + elementType);} / * * Returns all of the values comprising E. * The result is uncloned, cached, and shared by all callers. * / private static E [] getUniverse (Class elementType) {return SharedSecrets.getJavaLangAccess () .getEnumConstantsShared (elementType);} / * This class is used to serialize all EnumSet instances, regardless of * implementation type. It captures their "logical contents" and they * are reconstructed using public static factories. This is necessary * to ensure that the existence of a particular implementation type is * an implementation detail. * * @ serial include * / private static class SerializationProxy implements java.io.Serializable {/ * The element type of this enum set. * * @ serial * / private final Class elementType; / * * The elements contained in this enum set. * * @ serial * / private final Enum [] elements; SerializationProxy (EnumSet set) {elementType = set.elementType; elements = set.toArray (ZERO_LENGTH_ENUM_ARRAY);} private Object readResolve () {EnumSet result = EnumSet.noneOf (elementType); for (Enum e: elements) result.add ((E) e); return result;} private static final long serialVersionUID = 362491234563181265L } Object writeReplace () {return new SerializationProxy (this);} / / readObject method for the serialization proxy pattern / / See Effective Java, Second Ed., Item 78. Private void readObject (java.io.ObjectInputStream stream) throws java.io.InvalidObjectException {throw new java.io.InvalidObjectException ("Proxy required");}} these are all the contents of the article "sample Analysis of EnumSet Abstract classes in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report