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.

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.

All of the elements in an enum set must come from a single enum type that is specified, explicitly or implicitly, when the set is created. Enum sets are represented internally as bit vectors. This representation is extremely compact and efficient. The space and time performance of this class should be good enough to allow its use as a high-quality, typesafe alternative to traditional int-based "bit flags." Even bulk operations (such as containsAll and retainAll) should run very quickly if their argument is also an enum set.

98 questions
77
votes
10 answers

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,Size.XXL,Size.XXXL); for(Iterator it =…
Adam
  • 1,944
  • 3
  • 17
  • 19
63
votes
2 answers

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 x = EnumSet.copyOf(Collections.emptySet()); Which somewhat works but doesn't…
David162795
  • 1,846
  • 3
  • 15
  • 20
49
votes
5 answers

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: static int DOCUMENT_STATUS_NO_STATE = 0 static…
soappatrol
  • 533
  • 2
  • 6
  • 6
45
votes
1 answer

java.util.stream.Collectors with EnumSet Stream

I'm trying to use in place of bitmask below is the code public static Set fromBitFlags(int bitFlag) { return ALL_OPTS.stream().filter(a -> (a.ameityId & bitFlag) > 0).collect(Collectors.toSet()); } I would like to return EnumSet…
Somasundaram Sekar
  • 5,244
  • 6
  • 43
  • 85
43
votes
5 answers

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... rest)). As a workaround, I used the following…
qqilihq
  • 10,794
  • 7
  • 48
  • 89
34
votes
4 answers

Why does EnumSet have many overloaded "of" methods?

While going through the EnumSet of method, I have seen multiple overloaded implementations of of method: public static > EnumSet of(E e) public static > EnumSet of(E e1, E e2) . . public static
Somnath Musib
  • 3,548
  • 3
  • 34
  • 47
20
votes
2 answers

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 primitive type), and typesafe to boot. On the other…
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
16
votes
4 answers

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 question is this, since i use a enum set to hold my…
Marthin
  • 6,413
  • 15
  • 58
  • 95
16
votes
3 answers

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 reds = EnumSet.of(Suit.HEARTS, Suit.DIAMONDS); EnumSet blacks = EnumSet.of(Suit.CLUBS, Suit.SPADES); Give two…
Alex Spurling
  • 54,094
  • 23
  • 70
  • 76
13
votes
2 answers

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 faster than their HashMap counterparts. I have…
Geek
  • 26,489
  • 43
  • 149
  • 227
10
votes
5 answers

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 static final int STYLE_BOLD = 1 << 0; // 1 public…
Roalt
  • 8,330
  • 7
  • 41
  • 53
9
votes
1 answer

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: Creating an EnumSet mask with some…
Denis
  • 557
  • 5
  • 17
8
votes
4 answers

Check enumsets for same enum values

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?
gts13
  • 1,048
  • 1
  • 16
  • 29
8
votes
3 answers

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 MAN = 0x00000001; private static final int WOMAN =…
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
8
votes
1 answer

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 on. I have this readout from logcat, from debug…
That Guy
  • 103
  • 1
  • 8
1
2 3 4 5 6 7