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

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

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(&quot;some value&quot;),
  STRING2(&quot;some other value&quot;);
  STRING3(&quot;some third value&quot;);
  
  ...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&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值构建一个映射:

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&lt;String, SomeEnum&gt; map = 
    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:

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

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&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.

private static final Map&lt;String, SomeEnum&gt; stringToEnum = Arrays.stream(values())
	.collect(Collectors.toMap(SomeEnum::toString, e -&gt; e));

public static SomeEnum fromString(String name) {
	return stringToEnum.get(name);
}

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

List&lt;SomeEnum&gt; enumList = someStringList.stream()
	.map(SomeEnum::fromString)
	.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:

确定