I need to iterate on all the enum values, check if they were used to construct an int (called input
) and if so, add them to a Set (called usefulEnums
). I can either use streams API or iterate over all the enums to do this task. Is there any benefit of using Arrays.stream()
over the traditional approach of iterating over the values()
array?
enum TestEnum { VALUE1, VALUE2, VALUE3 };
Set<TestEnum> usefulEnums = new HashSet<>();
Arrays.stream(TestEnum.values())
.filter(t -> (input & t.getValue()) != 0)
.forEach(usefulEnums::add);
for (TestEnum t : TestEnum.values()) {
if ((input & t.getValue()) != 0) {
usefulEnums.add(t);
}
}
If you care for efficiency, you should consider:
Set<TestEnum> usefulEnums = EnumSet.allOf(TestEnum.class);
usefulEnums.removeIf(t -> (input & t.getValue()) == 0);
Note that when you have to iterate over all enum
constants of a type, using EnumSet.allOf(EnumType.class).stream()
avoids the array creation of EnumType.values()
entirely, however, most enum
types don’t have enough constants for this to make a difference. Further, the JVM’s optimizer may remove the temporary array creation anyway.
But for this specific task, where the result is supposed to be a Set<TestEnum>
, using an EnumSet
instead of a HashSet
may even improve subsequent operations working with the Set
. Creating an EnumSet
holding all constants and removing unintented constants like in the solution above, means just initializing a long
with 0b111
, followed by clearing the bits of nonmatching elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With