I have an Enum of Strings like this:
public enum SomeEnum {
STRING1("some value"),
STRING2("some other value");
STRING3("some third value");
...more strings...
private String name;
SomeEnum(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
And I have a List someStringList containing some Strings.
I want to iterate over someStringList and find the corresponding enums for it.
For example: The List contains the Strings "some value" and "some third value", then I would like to use the Java Stream-API to return me a List containing SomeEnum.STRING1 and SomeEnum.STRING3
Somehow I can't get this to work. I tried something like this:
List<SomeEnum> enumList = Stream.of(someStringList).forEach( s -> Stream.of(SomeEnum.values()).filter(w -> w.toString().equalsIgnoreCase(s)).collect(Collectors.toList()));
but this doesn't compile because it doesn't return anything. Any ideas?
findmethod in the accepted answer. Just stream over your list of strings andmapwith the result offind.SomeEnumvalues, filter it by checking if thesomeStringListcontains thenameof the enum value being filtered and collect the filtered enum values into a list