Java collections framework: Difference between revisions

Content deleted Content added
Set interface implementations: Add information for EnumSet
EnumSet class: Add more information about EnumSet
Line 191:
 
====EnumSet class====
The {{java|EnumSet}} class extends {{java|AbstractSet}}. The {{java|EnumSet}} class has no public constructors, and only contain static factory methods.{{sfn|Bloch|2018|loc=Chapter §5 Use EnumSet instead of bit fields|pp=5-9}}
 
{{java|EnumSet}} contains the static factory method {{java|EnumSet.of()}}.{{sfn|Bloch|2018|loc=Chapter §5 Use EnumSet instead of bit fields|pp=169-170}} This method is an aggregation method.{{sfn|Bloch|2018|loc=Chapter §5 Use EnumSet instead of bit fields|pp=5-9}} It takes in several parameters, takes into account of the type of the parameters, then returns an instance with the appropriate type.{{sfn|Bloch|2018|loc=Chapter §5 Use EnumSet instead of bit fields|pp=5-9}} As of 2018, In Java SE8 OpenJDK implementation uses two implementations of {{java|EnumSet}} which are invisible to the client, which are {{java|RegularEnumSet}} and {{java|JumboEnumSet}}.{{sfn|Bloch|2018|loc=Chapter §5 Use EnumSet instead of bit fields|pp=5-9}} If the {{java|RegularEnumSet}} no longer provided any performance benefits for small enum types, it could be removed from the library without negatively impacting the Java Collection Library.{{sfn|Bloch|2018|loc=Chapter §5 Use EnumSet instead of bit fields|pp=5-9}}
{{java|EnumSet}} is a good replacement for the ''bit fields'', which is a type of set, as described below.
 
{{java|EnumSet}} is a good replacement for the ''bit fields'', which is a type of set, as described below.{{sfn|Bloch|2018|loc=Chapter §5 Use EnumSet instead of bit fields|pp=169-170}}
 
Traditionally, whenever developers encountered elements of an enumerated type that needs to be placed in a set, the developer would use the ''int enum pattern'' in which every constant is assigned a different power of 2.{{sfn|Bloch|2018|loc=Chapter §5 Use EnumSet instead of bit fields|pp=169-170}} This bit representation enables the developer to use the bitwise OR operation, so that the constants can be combined into a set, also known as a ''bit field''.