Need of EnumSet / EnumMap over generified Set / Map
up vote
1
down vote
favorite
EnumSet/EnumMap
can be created by specifying the defined enum to produce set/map instance as shown in below sample code.
So far I read, difference between EnumSet/EnumMap
with that of Set/Map is that we cannot add objects other than the specified Enum
in the EnumSet/EnumMap
.
If this is the case, then just the generified Set/Map itself will be enough, isn't it?
Please find the EnumSet/EnumMap
and their respective generified Set/Map
as follows,
enum Value {
VALUE_1, VALUE_2, VALUE_3
};
public class Sample {
public static void main(String args) {
EnumSet<Value> enumSet = EnumSet.of(Value.VALUE_1);
Set<Value> enumGenerifiedSet = new HashSet<Value>();
enumGenerifiedSet.add(Value.VALUE_1);
EnumMap<Value, Integer> enumMap = new EnumMap<Value, Integer>(Value.class);
enumMap.put(Value.VALUE_1, 1);
Map<Value, Integer> enumGenerifiedMap = new HashMap<Value, Integer>();
enumGenerifiedMap.put(Value.VALUE_1, 1);
}
}
So can you please tell me what is the need of having EnumSet/EnumMap
eventhough we can able to create the set/map that is generified to the defined Enum?
Thanks in advance.
java oop enums set enumset
add a comment |
up vote
1
down vote
favorite
EnumSet/EnumMap
can be created by specifying the defined enum to produce set/map instance as shown in below sample code.
So far I read, difference between EnumSet/EnumMap
with that of Set/Map is that we cannot add objects other than the specified Enum
in the EnumSet/EnumMap
.
If this is the case, then just the generified Set/Map itself will be enough, isn't it?
Please find the EnumSet/EnumMap
and their respective generified Set/Map
as follows,
enum Value {
VALUE_1, VALUE_2, VALUE_3
};
public class Sample {
public static void main(String args) {
EnumSet<Value> enumSet = EnumSet.of(Value.VALUE_1);
Set<Value> enumGenerifiedSet = new HashSet<Value>();
enumGenerifiedSet.add(Value.VALUE_1);
EnumMap<Value, Integer> enumMap = new EnumMap<Value, Integer>(Value.class);
enumMap.put(Value.VALUE_1, 1);
Map<Value, Integer> enumGenerifiedMap = new HashMap<Value, Integer>();
enumGenerifiedMap.put(Value.VALUE_1, 1);
}
}
So can you please tell me what is the need of having EnumSet/EnumMap
eventhough we can able to create the set/map that is generified to the defined Enum?
Thanks in advance.
java oop enums set enumset
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
EnumSet/EnumMap
can be created by specifying the defined enum to produce set/map instance as shown in below sample code.
So far I read, difference between EnumSet/EnumMap
with that of Set/Map is that we cannot add objects other than the specified Enum
in the EnumSet/EnumMap
.
If this is the case, then just the generified Set/Map itself will be enough, isn't it?
Please find the EnumSet/EnumMap
and their respective generified Set/Map
as follows,
enum Value {
VALUE_1, VALUE_2, VALUE_3
};
public class Sample {
public static void main(String args) {
EnumSet<Value> enumSet = EnumSet.of(Value.VALUE_1);
Set<Value> enumGenerifiedSet = new HashSet<Value>();
enumGenerifiedSet.add(Value.VALUE_1);
EnumMap<Value, Integer> enumMap = new EnumMap<Value, Integer>(Value.class);
enumMap.put(Value.VALUE_1, 1);
Map<Value, Integer> enumGenerifiedMap = new HashMap<Value, Integer>();
enumGenerifiedMap.put(Value.VALUE_1, 1);
}
}
So can you please tell me what is the need of having EnumSet/EnumMap
eventhough we can able to create the set/map that is generified to the defined Enum?
Thanks in advance.
java oop enums set enumset
EnumSet/EnumMap
can be created by specifying the defined enum to produce set/map instance as shown in below sample code.
So far I read, difference between EnumSet/EnumMap
with that of Set/Map is that we cannot add objects other than the specified Enum
in the EnumSet/EnumMap
.
If this is the case, then just the generified Set/Map itself will be enough, isn't it?
Please find the EnumSet/EnumMap
and their respective generified Set/Map
as follows,
enum Value {
VALUE_1, VALUE_2, VALUE_3
};
public class Sample {
public static void main(String args) {
EnumSet<Value> enumSet = EnumSet.of(Value.VALUE_1);
Set<Value> enumGenerifiedSet = new HashSet<Value>();
enumGenerifiedSet.add(Value.VALUE_1);
EnumMap<Value, Integer> enumMap = new EnumMap<Value, Integer>(Value.class);
enumMap.put(Value.VALUE_1, 1);
Map<Value, Integer> enumGenerifiedMap = new HashMap<Value, Integer>();
enumGenerifiedMap.put(Value.VALUE_1, 1);
}
}
So can you please tell me what is the need of having EnumSet/EnumMap
eventhough we can able to create the set/map that is generified to the defined Enum?
Thanks in advance.
java oop enums set enumset
java oop enums set enumset
edited Nov 9 at 12:09
Andrew Tobilko
23.8k84078
23.8k84078
asked Nov 9 at 10:52
parthiban
1817
1817
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
The interfaces are nearly identical. Though, the performance and the underlying mechanisms are completely different.
The documentation is pretty clear on this:
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
andretainAll
) should run very quickly if their argument is also an enum set.
[...]
null
elements are not permitted. Attempts to insert anull
element will throwNullPointerException
. Attempts to test for the presence of anull
element or to remove one will, however, function properly.
[...]
Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be much faster than their
HashSet
counterparts. Even bulk operations execute in constant time if their argument is also an enum set.
java.util.EnumSet
, JDK 11
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
The interfaces are nearly identical. Though, the performance and the underlying mechanisms are completely different.
The documentation is pretty clear on this:
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
andretainAll
) should run very quickly if their argument is also an enum set.
[...]
null
elements are not permitted. Attempts to insert anull
element will throwNullPointerException
. Attempts to test for the presence of anull
element or to remove one will, however, function properly.
[...]
Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be much faster than their
HashSet
counterparts. Even bulk operations execute in constant time if their argument is also an enum set.
java.util.EnumSet
, JDK 11
add a comment |
up vote
2
down vote
The interfaces are nearly identical. Though, the performance and the underlying mechanisms are completely different.
The documentation is pretty clear on this:
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
andretainAll
) should run very quickly if their argument is also an enum set.
[...]
null
elements are not permitted. Attempts to insert anull
element will throwNullPointerException
. Attempts to test for the presence of anull
element or to remove one will, however, function properly.
[...]
Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be much faster than their
HashSet
counterparts. Even bulk operations execute in constant time if their argument is also an enum set.
java.util.EnumSet
, JDK 11
add a comment |
up vote
2
down vote
up vote
2
down vote
The interfaces are nearly identical. Though, the performance and the underlying mechanisms are completely different.
The documentation is pretty clear on this:
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
andretainAll
) should run very quickly if their argument is also an enum set.
[...]
null
elements are not permitted. Attempts to insert anull
element will throwNullPointerException
. Attempts to test for the presence of anull
element or to remove one will, however, function properly.
[...]
Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be much faster than their
HashSet
counterparts. Even bulk operations execute in constant time if their argument is also an enum set.
java.util.EnumSet
, JDK 11
The interfaces are nearly identical. Though, the performance and the underlying mechanisms are completely different.
The documentation is pretty clear on this:
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
andretainAll
) should run very quickly if their argument is also an enum set.
[...]
null
elements are not permitted. Attempts to insert anull
element will throwNullPointerException
. Attempts to test for the presence of anull
element or to remove one will, however, function properly.
[...]
Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be much faster than their
HashSet
counterparts. Even bulk operations execute in constant time if their argument is also an enum set.
java.util.EnumSet
, JDK 11
edited Nov 9 at 11:06
answered Nov 9 at 11:00
Andrew Tobilko
23.8k84078
23.8k84078
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224310%2fneed-of-enumset-enummap-over-generified-set-map%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown