I want to convert an ArrayList<String> to Set<ScopeItem> with Java streams.
ScopeItem is a enum;
items is an ArrayList<String>;
Set<ScopeItem> scopeItems = items.stream()
.map(scopeString -> ScopeItem.valueOf(scopeString))
.filter(Objects::nonNull)
.collect(Collectors.toSet());
On a string that isn't in the enum this throws the following:
java.lang.IllegalArgumentException: No enum const...
Ideally, I would like to skip past any Strings that don't match.
I think maybe a using flatmap? Any ideas how to do it?
ScopeItem, I suggest adding a contains() method to your enumerationScopeItems used for, you actually might want it to "blow up" as soon as possible, as ignoring silently values might be the source of really subtle bugs. So, maybe just convert default exception to something with more useful message.