如何在Java中使用流(Streams)从链表(linkedlist)中获取对象的元素。

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

How to get an element of an object from a linkedlist using streams in Java

问题

Consider a class ABC

class ABC {
  int id;
  String text;
  // getter & setter methods..
}

A list of instances of ABC is collected in a linked list.

LinkedList<ABC>

Now, I have an input int n. Based on the input n, I need to check the list of ABC instances where I can get a match (n == id).

Based on the match, I will have to retrieve the corresponding String text from the same instance of ABC.

For example:

LinkedList<ABC> list = new LinkedList<>();
list.add(new ABC(1, "Eagle"));
list.add(new ABC(2, "Tiger"));
list.add(new ABC(3, "Rabbit"));

If the input n = 3,
then I need to get the below result:
"Rabbit"

I tried to use streams as below:

list.stream().filter(p -> p.getId() == n).map(ABC::getText);

But I want only String as an answer from the above line.

英文:

Consider a class ABC

class ABC{
  int id;
  String text;
  getter &amp; setter methods..
}

A list of instances of ABC is collected in a linked list.

Linkedlist&lt;ABC&gt;

Now, I have an input int n. Based on the input n, I need to check list of ABC instances where I can get a match (n == d)

Based on the match, I will have to retrieve the corresponding String text from the same instance of ABC.

For example:

Linkedlist&lt;ABC&gt; list = new Linkedlist&lt;&gt;();
list.add(new ABC(1,&quot;Eagle&quot;);
list.add(new ABC(2,&quot;Tiger&quot;);
list.add(new ABC(3,&quot;Rabbit&quot;);

if the input n = 3,
then I need to get the below result:
"Rabbit"

I tried to use streams as below:

list.stream().filter(p -&gt; p.getId() == n).map(ABC:: getText);

But I want only String as an answer from the above line.

答案1

得分: 1

  • 使用 findFirst 获取一个 ABC 实例,它返回一个 Optional<ABC>
  • 使用 orElseThrow 获取真正的 ABC 或在没有匹配项时引发异常
  • 使用 getText 一次来获取其文本
String result = list.stream().filter(p -> p.getId() == n).findFirst().orElseThrow().getText();
英文:

Use

  • findFirst to get one ABC instance, it returns an Optional&lt;ABC&gt;
  • orElseThrow to get the real ABC or raise an exception is no match
  • getText once to get its text

<!-- -->

String result = list.stream().filter(p -&gt; p.getId() == n).findFirst().orElseThrow().getText();

答案2

得分: 1

使用 findFirst() 并注意返回类型是 Optional 实例。

String result = list.stream()
                   .filter(p -> p.getId() == n)
                   .map(ABC::getText)
                   .findFirst()
                   .orElse(null);
英文:

use findFirst() and be aware return type is an Optional instance.

 String result = list.stream()
                   .filter(p -&gt; p.getId() == n)
                   .map(ABC::getText)
                   .findFirst()
                   .orElse(null);

答案3

得分: 1

你可以使用 Stream#findFirst,以及 Optional#orElse

System.out.println(
    list.stream().filter(abc -&gt; abc.id == n)
                 .map(abc -&gt; abc.text)
                 .findFirst()
                 .orElse(null));

输出,其中 n 为 3。

Rabbit
英文:

You can use Stream#findFirst, with the Optional#orElse.

System.out.println(
    list.stream().filter(abc -&gt; abc.id == n)
                 .map(abc -&gt; abc.text)
                 .findFirst()
                 .orElse(null));

Output, where n is 3.

Rabbit

huangapple
  • 本文由 发表于 2023年6月15日 01:36:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476209.html
匿名

发表评论

匿名网友

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

确定