返回枚举中包含的给定字符串列表的枚举数组,使用流操作。

huangapple go评论102阅读模式
英文:

Return enum array for a list of given strings contained in the enum using streams

问题

  1. public enum SomeEnum {
  2. STRING1("some value"),
  3. STRING2("some other value"),
  4. STRING3("some third value");
  5. ...more strings...
  6. private String name;
  7. SomeEnum(String name) {
  8. this.name = name;
  9. }
  10. @Override
  11. public String toString() {
  12. return name;
  13. }
  14. }
  15. // ...
  16. List<String> someStringList = ...; // Your list of strings
  17. List<SomeEnum> enumList = someStringList.stream()
  18. .map(s -> Stream.of(SomeEnum.values())
  19. .filter(w -> w.toString().equalsIgnoreCase(s))
  20. .findFirst()
  21. .orElse(null))
  22. .filter(Objects::nonNull)
  23. .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:

  1. public enum SomeEnum {
  2. STRING1(&quot;some value&quot;),
  3. STRING2(&quot;some other value&quot;);
  4. STRING3(&quot;some third value&quot;);
  5. ...more strings...
  6. private String name;
  7. SomeEnum(String name) {
  8. this.name = name;
  9. }
  10. @Override
  11. public String toString() {
  12. return name;
  13. }

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:

  1. List&lt;SomeEnum&gt; enumList = Stream.of(someStringList).forEach( s -&gt; Stream.of(SomeEnum.values()).filter(w -&gt; w.toString().equalsIgnoreCase(s)).collect(Collectors.toList()));

but this doesn't compile because it doesn't return anything. Any ideas?

答案1

得分: 1

从字符串到相应的SomeEnum值构建一个映射:

  1. Map<String, SomeEnum> map =
  2. Arrays.stream(SomeEnum.values()).collect(toMap(SomeEnum::toString, s -> s));

(可以执行一次并存储)

然后您可以在此映射中查找:

  1. List<SomeEnum> enumList = someStringList.stream().map(map::get).collect(toList());

(您可能希望处理字符串在映射中找不到的情况,例如,您可以抛出异常或删除这些元素)。

英文:

Build a map from the string to the corresponding SomeEnum value:

  1. Map&lt;String, SomeEnum&gt; map =
  2. Arrays.stream(SomeEnum.values()).collect(toMap(SomeEnum::toString, s -&gt; s));

(This can be done once and stored)

Then you can look up in this:

  1. List&lt;SomeEnum&gt; 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 的公共静态方法,该方法根据给定的字符串返回枚举常量(如果存在)。

  1. private static final Map<String, SomeEnum> stringToEnum = Arrays.stream(values())
  2. .collect(Collectors.toMap(SomeEnum::toString, e -> e));
  3. public static SomeEnum fromString(String name) {
  4. return stringToEnum.get(name);
  5. }

然后在你的客户端代码中使用它。以下是示例代码:

  1. List<SomeEnum> enumList = someStringList.stream()
  2. .map(SomeEnum::fromString)
  3. .collect(Collectors.toList());
英文:

In your SomeEnum you can build a Map&lt;String, SomeEnum&gt; 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.

  1. private static final Map&lt;String, SomeEnum&gt; stringToEnum = Arrays.stream(values())
  2. .collect(Collectors.toMap(SomeEnum::toString, e -&gt; e));
  3. public static SomeEnum fromString(String name) {
  4. return stringToEnum.get(name);
  5. }

Then use it in your client code. Here's how it looks.

  1. List&lt;SomeEnum&gt; enumList = someStringList.stream()
  2. .map(SomeEnum::fromString)
  3. .collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年4月9日 21:16:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/61122061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定