英文:
Return enum array for a list of given strings contained in the enum using streams
问题
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;
}
}
// ...
List<String> someStringList = ...; // Your list of strings
List<SomeEnum> enumList = someStringList.stream()
.map(s -> Stream.of(SomeEnum.values())
.filter(w -> w.toString().equalsIgnoreCase(s))
.findFirst()
.orElse(null))
.filter(Objects::nonNull)
.collect(Collectors.toList());
Please note that the provided code is a translation of the original Java code you provided. Make sure to replace the placeholders with actual code as needed.
英文:
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<String> 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<SomeEnum> 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?
答案1
得分: 1
从字符串到相应的SomeEnum
值构建一个映射:
Map<String, SomeEnum> map =
Arrays.stream(SomeEnum.values()).collect(toMap(SomeEnum::toString, s -> s));
(可以执行一次并存储)
然后您可以在此映射中查找:
List<SomeEnum> enumList = someStringList.stream().map(map::get).collect(toList());
(您可能希望处理字符串在映射中找不到的情况,例如,您可以抛出异常或删除这些元素)。
英文:
Build a map from the string to the corresponding SomeEnum
value:
Map<String, SomeEnum> map =
Arrays.stream(SomeEnum.values()).collect(toMap(SomeEnum::toString, s -> s));
(This can be done once and stored)
Then you can look up in this:
List<SomeEnum> enumList = someStringList.stream().map(map::get).collect(toList());
(You may or may not want to handle the case where a string isn't found in the map: for example, you could throw an exception, or drop such elements).
答案2
得分: 1
在你的 SomeEnum
中,你可以构建一个从字符串名称到枚举的 Map<String, SomeEnum>
,并在类加载时初始化它。然后在你的 SomeEnum
中声明一个名为 fromString
的公共静态方法,该方法根据给定的字符串返回枚举常量(如果存在)。
private static final Map<String, SomeEnum> stringToEnum = Arrays.stream(values())
.collect(Collectors.toMap(SomeEnum::toString, e -> e));
public static SomeEnum fromString(String name) {
return stringToEnum.get(name);
}
然后在你的客户端代码中使用它。以下是示例代码:
List<SomeEnum> enumList = someStringList.stream()
.map(SomeEnum::fromString)
.collect(Collectors.toList());
英文:
In your SomeEnum
you can build a Map<String, SomeEnum>
from string name to enum and initialize it during class loading time. Then declare a public static method in your SomeEnum
named fromString
which returns an enum constant for a given string if one exists.
private static final Map<String, SomeEnum> stringToEnum = Arrays.stream(values())
.collect(Collectors.toMap(SomeEnum::toString, e -> e));
public static SomeEnum fromString(String name) {
return stringToEnum.get(name);
}
Then use it in your client code. Here's how it looks.
List<SomeEnum> enumList = someStringList.stream()
.map(SomeEnum::fromString)
.collect(Collectors.toList());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论