EnumSet related knowledge

EnumSet enumeration is designed specifically for a collection class, all the elements in the EnumSet enumeration value must be specified enumeration type, the enumeration type specified explicitly or implicitly when creating EnumSet.

  1. EnumSet the collection elements are ordered, EnumSet enumerated values ​​to Enum class in order to determine the order of the collection of elements.
  2. EnumSet bits stored within the vector, such storage is extremely compact, efficient, and therefore the object EnumSet small memory footprint, and good operating efficiency. Especially when batch (such as calling containsAll () retainAll, and () method), if the parameters are set EnumSet, the execution speed of the batch operation is also very fast.
  3. EnumSet collection does not permit null elements to join, if you try to insert null elements EnumSet NullPointerException will be thrown.
  4. EnumSet without exposing any class constructor to create an instance of the class, the program should be created by the object class EnumSet It provides methods.
  5. If you just want to determine whether EnumSet contain null null elements or try to delete elements will not throw an exception, simply delete operation will return false, because there is no null element is deleted.

Methods Introduction:

  • EnumSet allOf (Class elementType): Create a EnumSet enumeration class specifies a set of all enumeration values.
  • EnumSet complementOf (EnumSet e): Creates an element of the type specified in the same element type EnumSet EnumSet set, a new set contains EnumSet, such enumeration class enumeration value remaining original EnumSet not included in the collection (i.e., the new EnumSet EnumSet set and the original set of collection elements add up all of the enumeration values enumeration class).
  • EnumSet copyOf (Collection c): Use to create a common set of EnumSet collection.
  • EnumSet copyOf (EnumSet e): Create a EnumSet designated with the same element type, the same set of elements EnumSet set.
  • EnumSet noneOf (Class elementType): create an element type as the specified enum type empty EnumSet.
  • EnumSet of (E first, E ... rest): create EnumSet a set of one or more enumerated values, passed multiple enumeration values must belong to the same class enumeration.
  • EnumSet range (E from, E to ): Creates an enum value from from to to EnumSet all enumeration values within the range of enumeration values collection.

Examples EnumSet

Guess you like

Origin blog.csdn.net/qq_43776742/article/details/91557188