How To Role Enumset Inwards Coffee Amongst Example
EnumSet is i of the specialized implementation of Set interface for an enumeration type, introduced inwards Java 1.5 along amongst enumeration type itself. Programmer ofttimes stores Enum into mutual collection classes e.g. HashSet or ArrayList, to a greater extent than ofttimes than non because they are unaware of this footling gem. Even I wasn't aware of this course of report few years agone until I come upwards across i of the finest books for Java programmers, bitwise operator. Since Enum constants are unique as well as possess got pre-defined length, equally y'all tin non define a novel enum constant at runtime; it allows Java API designer to highly optimize EnumSet.
If y'all await closely, EnumSet likewise teaches y'all a span of expert blueprint practices to create flexible as well as maintainable code. For example, EnumSet is an abstract class, as well as it provides lots of static mill method to create an instance of EnumSet e.g. EnumSet.of(...). This method takes wages of method overloading as well as variable argument to render Set of alone provided Enum constants.
Second expert affair is at that topographic point are 2 concrete subclasses of EnumSet e.g. RegularEnumSet and JumboEnumSet, but both are package-private, which way a customer tin non role them directly.
The alone way to role them is via EnumSet. At runtime, depending upon the key size of requested Enum, implementation automatically create upwards one's remove heed which implementation to use. This provides y'all immense flexibility as well as command to curl out a novel amend version of EnumSet implementation without whatsoever alter on the customer side.
Anyway, this article is non only almost blueprint lessons from this course of report but to a greater extent than importantly how as well as when to role EnumSet inwards Java program. I possess got shared a uncomplicated illustration to demonstrate the mightiness of EnumSet, which y'all volition run into inwards side yesteryear side section.
In this example, nosotros possess got used basic colours RED, GREEN as well as BLUE to create a custom color yesteryear using EnumSet. As I said, EnumSet is rattling expert for combining effects, whether it's text styles e.g. BOLD and UNDERLINE, equally described inwards Effective Java, or combining basic colours to create custom color here. If y'all possess got MS paint, y'all tin fifty-fifty attempt the combination of colours e.g. y'all tin combine RED and BLUE to create YELLOW, combine all 3 to create WHITE, or combining RED and BLUE to create PINK.
1) EnumSet is a exceptional Set implementation, alone applicable for Enums inwards Java, but y'all tin alone shop instances of the unmarried enum type. Adding an instance of dissimilar enum volition outcome inwards compile fourth dimension error, equally EnumSet render type-safety.
2) EnumSet internal representation is extremely efficient as well as represented equally chip vectors. Library itself chooses i of 2 implementations available depending upon the size of a key universe. RegularEnumSet has chosen if a number of instances are less than 64, otherwise JumboEnumSet is used.
3) As described inwards Effective Java, EnumSet tin survive safely used equally a type-safe option of traditional int based "bit flags". EnumSet provides much-needed readability, without compromising performance.
4) Iterator returned yesteryear EnumSet traverse the elements inwards their natural order, i.e. the social club on which enum constants are declared, or the social club returned yesteryear ordinal() method.
5) An EnumSet iterator is weakly consistent as well as never throws ConcurrentModificationException, as well as may or may non present the number of whatsoever alteration to the Set acre the iteration is inwards progress.
6) EnumSet is likewise non synchronized inwards Java. Though if y'all need, y'all tin brand EnumSet synchronized similar to other collection yesteryear using utility methods from Collections class. Here is how to create that :
Set<YourEnum> s = Collections.synchronizedSet(EnumSet.noneOf(YourEnum.class));
7) EnumSet is an abstract class, which way y'all cannot create its instance using new() operator. This is genuinely carefully idea to render exceptional implementation, as well as that's why EnumSet provides several static mill methods for creating instance e.g. noneOf() returns an empty EnumSet amongst specified enum type, EnumSet.of(....) returns Set of specified enum constants as well as allOf() method creates an enum fix containing all elements of specified enum.
8) EnumSet likewise provides methods similar complementOf(EnumSet es) as well as copyOf(EnumSet es) to create EnumSet from other EnumSet, coplementOf() returns enum fix amongst containing all the elements of this type that are non contained inwards the specified set.
That's all almost EnumSet inwards Java. It's i of the useful class, which is perfect for sure as shooting role cases, but it likewise teaches a lot of lesson on course of report blueprint as well as role of the package. I genuinely similar how they shielded implementation course of report RegularEnumSet as well as JumboEnumSet as well as provides mill methods to create EnumSet. This way tidings of choosing implementation remains amongst the library itself, yesteryear making both of these classes package-private, at that topographic point is no worry of the customer direct accessing them. You tin role EnumSet to transcend arguments to methods as well as to create meaningful groups from enum.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures as well as Algorithms: Deep Dive Using Java
Algorithms as well as Data Structures - Part 1 as well as 2
Data Structures inwards Java nine yesteryear Heinz Kabutz
If y'all await closely, EnumSet likewise teaches y'all a span of expert blueprint practices to create flexible as well as maintainable code. For example, EnumSet is an abstract class, as well as it provides lots of static mill method to create an instance of EnumSet e.g. EnumSet.of(...). This method takes wages of method overloading as well as variable argument to render Set of alone provided Enum constants.
Second expert affair is at that topographic point are 2 concrete subclasses of EnumSet e.g. RegularEnumSet and JumboEnumSet, but both are package-private, which way a customer tin non role them directly.
The alone way to role them is via EnumSet. At runtime, depending upon the key size of requested Enum, implementation automatically create upwards one's remove heed which implementation to use. This provides y'all immense flexibility as well as command to curl out a novel amend version of EnumSet implementation without whatsoever alter on the customer side.
Anyway, this article is non only almost blueprint lessons from this course of report but to a greater extent than importantly how as well as when to role EnumSet inwards Java program. I possess got shared a uncomplicated illustration to demonstrate the mightiness of EnumSet, which y'all volition run into inwards side yesteryear side section.
EnumSet Example inwards Java
In this example, nosotros possess got used basic colours RED, GREEN as well as BLUE to create a custom color yesteryear using EnumSet. As I said, EnumSet is rattling expert for combining effects, whether it's text styles e.g. BOLD and UNDERLINE, equally described inwards Effective Java, or combining basic colours to create custom color here. If y'all possess got MS paint, y'all tin fifty-fifty attempt the combination of colours e.g. y'all tin combine RED and BLUE to create YELLOW, combine all 3 to create WHITE, or combining RED and BLUE to create PINK.import java.util.EnumSet; import java.util.Set; /** * Simple Java Program to demonstrate how to role EnumSet. * It has closed to interesting role cases as well as it's specialized collection for * Enumeration types. Using Enum amongst EnumSet volition laissez passer on y'all far amend * functioning than using Enum amongst HashSet, or LinkedHashSet. * * @author Javin Paul */ world class EnumSetDemo { private enum Color { RED(255, 0, 0), GREEN(0, 255, 0), BLUE(0, 0, 255); private int r; private int g; private int b; private Color(int r, int g, int b) { this.r = r; this.g = g; this.b = b; } world int getR() { return r; } world int getG() { return g; } world int getB() { return b; } } world static void main(String args[]) { // this volition pull business inwards yellowish color EnumSet<Color> yellowish = EnumSet.of(Color.RED, Color.GREEN); drawLine(yellow); // RED + GREEN + BLUE = WHITE EnumSet<Color> white = EnumSet.of(Color.RED, Color.GREEN, Color.BLUE); drawLine(white); // RED + BLUE = PINK EnumSet<Color> pinkish = EnumSet.of(Color.RED, Color.BLUE); drawLine(pink); } world static void drawLine(Set<Color> colors) { System.out.println("Requested Colors to pull lines : " + colors); for (Color c : colors) { System.out.println("drawing business inwards color : " + c); } } } Output: Requested Colors to draw lines : [RED, GREEN] drawing business inwards color : RED drawing business inwards color : GREEN Requested Colors to pull lines : [RED, GREEN, BLUE] drawing business inwards color : RED drawing business inwards color : GREEN drawing business inwards color : BLUE Requested Colors to pull lines : [RED, BLUE] drawing business inwards color : RED drawing business inwards color : BLUE
Important properties of EnumSet inwards Java
Like whatsoever other Collection class, EnumSet likewise has closed to exceptional properties, which assist y'all to create upwards one's remove heed when to role EnumSet inwards your Java application. Keep a banknote of these properties, behaviours:1) EnumSet is a exceptional Set implementation, alone applicable for Enums inwards Java, but y'all tin alone shop instances of the unmarried enum type. Adding an instance of dissimilar enum volition outcome inwards compile fourth dimension error, equally EnumSet render type-safety.
2) EnumSet internal representation is extremely efficient as well as represented equally chip vectors. Library itself chooses i of 2 implementations available depending upon the size of a key universe. RegularEnumSet has chosen if a number of instances are less than 64, otherwise JumboEnumSet is used.
3) As described inwards Effective Java, EnumSet tin survive safely used equally a type-safe option of traditional int based "bit flags". EnumSet provides much-needed readability, without compromising performance.
4) Iterator returned yesteryear EnumSet traverse the elements inwards their natural order, i.e. the social club on which enum constants are declared, or the social club returned yesteryear ordinal() method.
5) An EnumSet iterator is weakly consistent as well as never throws ConcurrentModificationException, as well as may or may non present the number of whatsoever alteration to the Set acre the iteration is inwards progress.
6) EnumSet is likewise non synchronized inwards Java. Though if y'all need, y'all tin brand EnumSet synchronized similar to other collection yesteryear using utility methods from Collections class. Here is how to create that :
Set<YourEnum> s = Collections.synchronizedSet(EnumSet.noneOf(YourEnum.class));
7) EnumSet is an abstract class, which way y'all cannot create its instance using new() operator. This is genuinely carefully idea to render exceptional implementation, as well as that's why EnumSet provides several static mill methods for creating instance e.g. noneOf() returns an empty EnumSet amongst specified enum type, EnumSet.of(....) returns Set of specified enum constants as well as allOf() method creates an enum fix containing all elements of specified enum.
8) EnumSet likewise provides methods similar complementOf(EnumSet es) as well as copyOf(EnumSet es) to create EnumSet from other EnumSet, coplementOf() returns enum fix amongst containing all the elements of this type that are non contained inwards the specified set.
That's all almost EnumSet inwards Java. It's i of the useful class, which is perfect for sure as shooting role cases, but it likewise teaches a lot of lesson on course of report blueprint as well as role of the package. I genuinely similar how they shielded implementation course of report RegularEnumSet as well as JumboEnumSet as well as provides mill methods to create EnumSet. This way tidings of choosing implementation remains amongst the library itself, yesteryear making both of these classes package-private, at that topographic point is no worry of the customer direct accessing them. You tin role EnumSet to transcend arguments to methods as well as to create meaningful groups from enum.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures as well as Algorithms: Deep Dive Using Java
Algorithms as well as Data Structures - Part 1 as well as 2
Data Structures inwards Java nine yesteryear Heinz Kabutz

Komentar
Posting Komentar