Questions tagged [enumset]

A specialized Set implementation for use with enum types. EnumSet class exists to take advantage of the efficient implementations that are possible when the number of possible elements is fixed and a unique index can be assigned to each.

Filter by
Sorted by
Tagged with
72votes
10answers
62kviews

What does EnumSet really mean?

I have the following example: import java.util.EnumSet; import java.util.Iterator; public class SizeSet { public static void main(String[] args) { EnumSet largeSize = EnumSet.of(Size.XL,...
user avatar
  • 1,884
50votes
2answers
20kviews

How to create empty EnumSet?

I am struggling with EnumSet as it surprisingly doesn't have a simple constructor and its methods doesn't like null values. What I came up with: EnumSet<MyClass> x = EnumSet.copyOf(Collections.&...
user avatar
  • 1,696
48votes
5answers
36kviews

Implementing a bitfield using java enums

I maintain a large document archive and I often use bit fields to record the status of my documents during processing or when validating them. My legacy code simply uses static int constants such as: ...
user avatar
42votes
5answers
14kviews

EnumSet from array, shortest variant?

I need an EnumSet from an array (which is given through a varargs method parameter). First, I was surprised that there is no varargs constructor method in EnumSet (there is EnumSet#of(E first, E... ...
user avatar
  • 9,741
40votes
1answer
13kviews

java.util.stream.Collectors with EnumSet Stream

I'm trying to use in place of bitmask below is the code public static Set<Amenities> fromBitFlags(int bitFlag) { return ALL_OPTS.stream().filter(a -> (a.ameityId & bitFlag) > 0)....
user avatar
34votes
4answers
1kviews

Why does EnumSet have many overloaded "of" methods?

While going through the EnumSet<E> of method, I have seen multiple overloaded implementations of of method: public static <E extends Enum<E>> EnumSet<E> of(E e) public static ...
user avatar
20votes
2answers
2kviews

Justification for using a bitfield instead of EnumSet in modern Java 8 API

EnumSet, as old as the enum itself (both since Java 5), is supposed to be a noncompromizing replacement for the use case of bitfields: as fast and lean as the bitfield (well, except for not being a ...
user avatar
16votes
3answers
11kviews

Combining Java EnumSets

If I have an Enum, I can create an EnumSet using the handy EnumSet class enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES } EnumSet<Suit> reds = EnumSet.of(Suit.HEARTS, Suit.DIAMONDS); EnumSet<...
user avatar
13votes
2answers
4kviews

Why an EnumSet or an EnumMap is likely to be more performant than their hashed counterparts?

The following is from the Implementation Note section of Java doc of EnumMap : Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be ...
user avatar
  • 25k
12votes
4answers
34kviews

java enums ordering

Im using java enums to define how to render a modal window with buttons (Vaadin handles the rendering). My problem is that when I run the gui my buttons comes in a randomized order each time. So my ...
user avatar
  • 6,219
9votes
1answer
2kviews

EnumSet serialization

I've just lost a couple of hours debugging my app, and I believe I've stumbled upon a (another one o_O) Java bug... sniff... I hope it is not, because this would be sad :( I'm doing the following: ...
user avatar
  • 527
9votes
5answers
19kviews

Best practice of using flags in Java method

What's the best practice for specifying flags in a Java method? I've seen SWT using int as bitfields, like: (example partially from "Effective Java, 2nd Ed." page 159): public class Text { public ...
user avatar
  • 8,158
8votes
4answers
3kviews

Check enumsets for same enum values [duplicate]

I have two EnumSets. EnumSet.of(A1, A2, A3); EnumSet.of(A3, A4, A5, A6); I want to find which values exist in both sets. (In that case, A3.) Is there any quick way to do that?
user avatar
  • 968
8votes
1answer
8kviews

Java - EnumSet.add(enum), throws NullPointerException

This is in Java, cross platform and being debugged on a computer running Ubuntu Oneric with OpenJDK installed as my runtime. I have an EnumSet for checking inside of in a class in a game I'm working ...
user avatar
  • 103
8votes
3answers
3kviews

Switch on EnumSet

The old way, if we wanted to switch on some complicated bitmask, we could easily do it like this (a random example from the top of my head just to demonstrate the issue): private static final int ...
user avatar

15 30 50 per page
1
2 3 4 5
7