Reputation: 1571
private enum EnumVals {
FIRST(new String[]{"a","b"}),
SECOND(new String[]{"a","b","c","d"}),
THIRD(new String[]{"d","e"});
private String[] vals;
EnumVals(String[] q) {
vals=q;
}
public String[] getValues(){
return vals;
}
};
What I need is the unique combined list of all EnumVals.getValues().
String[] allVals = {"a","b","c","d","e"}
I did something like below, but it throws error :
Stream.of(EnumVals.values()).map(w->w.getValues()).collect(Collectors.toCollection(HashSet::new)).toArray(new String[0]);
Upvotes: 3
Views: 67
Reputation: 28183
You need to use flatMap
to flatten the arrays. Additionally, you can use distinct()
on the stream instead of collecting into a HashSet
.
Arrays.stream(EnumVals.values())
.map(EnumVals::getValues)
.flatMap(Arrays::stream)
.distinct()
.toArray(String[]::new);
Upvotes: 8