获取Java 11中stream filter()的第一个匹配元素如何实现?

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

How get the first matching element from stream filter() in JAVA 11?

问题

如何从JAVA 11中的stream filter()中获取第一个匹配的元素?

List<Address> addresses = List.of(
    new Address(1, "办公室", "AAAAA"), new Address(2, "通讯", "BBBBB"),
    new Address(3, "住所", "CCCC"), new Address(4, "隔壁", "DDDD"));

如何根据优先级检索第一个匹配的地址?

addresses.stream().filter(a -> a.getType().equals("住所")).findAny()
    .orElseGet(() -> addresses.stream().filter(a -> a.getType()
        .equals("通讯")).findAny()
        .orElseGet(() -> addresses.stream().filter(a -> a.getType()
            .equals("办公室")).findAny()
            .orElseGet(() -> addresses.stream().filter(a -> a.getType()
                .equals("隔壁")).findAny().orElse(null))));

如何简化上述代码以根据其Type获取Address

如果列表中有住所类型,则返回Address,否则按照以下优先级进行:通讯办公室隔壁

STREAM中做这个最好的方法是什么?

英文:

How get the first matching element from stream filter() in JAVA 11?

List&lt;Address&gt; addresses = List.of(
    new Address(1, &quot;Office&quot;, &quot;AAAAA&quot;), new Address(2, &quot;Correspondence&quot;, &quot;BBBBB&quot;),
    new Address(3, &quot;Residence&quot;, &quot;CCCC&quot;), new Address(4, &quot;Next Door&quot;, &quot;DDDD&quot;));

How to retrieve the first matching address based on the priorities?

addresses.stream().filter(a-&gt;a.getType().equals(&quot;Residence&quot;)).findAny()
  .orElseGet(() -&gt; addresses.stream().filter(a-&gt;a.getType()
       .equals(&quot;Correspondence&quot;)).findAny()
            .orElseGet(()-&gt;addresses.stream().filter(a-&gt;a.getType()
               .equals(&quot;Correspondence&quot;)).findAny().orElseGet(null)));

How to simplify the above code to get the Address based on it's Type?

If the Residence type is available in the List then return Address, else next priorities are followed Correspondence, Office, and Next Door.

What is the best way to do it in STREAM?

答案1

得分: 4

以下是您要翻译的内容:

使用类型列表,按所需优先级的顺序,既用于筛选流,也用作您在流上使用 min() 的比较器的基础:

List<String> types = List.of("Residence", "Correspondance", "Office", "Next Door");

addresses.stream()
    .filter(a -> types.contains(a.getType()))
    .min(comparingInt(a -> types.indexOf(a.getType())));

通常,您会使用一个常量来表示该列表,以便不需要每次创建:

private static final List<String> ADDRESS_TYPES = List.of("Residence", "Correspondance", "Office", "Next Door");

附注: "Correspondence" 的拼写是 ...ence,而不是 ...ance。

英文:

Use a list of the types, in order of desired precedence, to both filter the stream and as the basis of a comparator you use with min() on the stream:

List&lt;String&gt; types = List.of(&quot;Residence&quot;, &quot;Correspondance&quot;, &quot;Office&quot;, &quot;Next Door&quot;);

addresses.stream()
    .filter(a -&gt; types.contains(a.getType()))
    .min(comparingInt(a -&gt; types.indexOf(a.getType())));

——

You would typically use a constant for the list so it didn’t need to be created every time.

private static final List&lt;String&gt; ADDRESS_TYPES = List.of(&quot;Residence&quot;, &quot;Correspondance&quot;, &quot;Office&quot;, &quot;Next Door&quot;);

——-

p.s "Correspondence" is spelled ...ence, not ...ance

答案2

得分: 2

这是你要翻译的代码部分:

private int priority(Address addr) {
    switch (addr.getType())
        case "Residence":
            return 1;
        case "Correspondance":
            return 2;
        case "Office":
            return 3;
        case "Next Door":
            return 4;
        default:
            return 5;
}
}

private Optional<Address> bestAddress(List<Address> addresses) {
    return
       addresses.stream().min(Comparator.comparingInt(this::priority));
}

如果需要更多翻译,请告诉我。

英文:
private int priority(Address addr) {
      switch (addr.getType())
        case &quot;Residence&quot;:
            return 1;
        case &quot;Correspondance&quot;:
            return 2;
        case &quot;Office&quot;:
            return 3;
        case &quot;Next Door&quot;:
            return 4;
        default:
            return 5;
      }
}

private Optional&lt;Address&gt; bestAddress(List&lt;Address&gt; addresses) {
    return
       addresses.stream().min(Comparator.comparingInt(this::priority));
}

Edited as per comment. Thanks.

huangapple
  • 本文由 发表于 2020年8月13日 22:06:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63396970.html
匿名

发表评论

匿名网友

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

确定