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

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

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

问题

Consider a class ABC

  1. class ABC {
  2. int id;
  3. String text;
  4. // getter & setter methods..
  5. }

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

  1. 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:

  1. LinkedList<ABC> list = new LinkedList<>();
  2. list.add(new ABC(1, "Eagle"));
  3. list.add(new ABC(2, "Tiger"));
  4. 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:

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

  1. class ABC{
  2. int id;
  3. String text;
  4. getter &amp; setter methods..
  5. }

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

  1. 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:

  1. Linkedlist&lt;ABC&gt; list = new Linkedlist&lt;&gt;();
  2. list.add(new ABC(1,&quot;Eagle&quot;);
  3. list.add(new ABC(2,&quot;Tiger&quot;);
  4. 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:

  1. 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 一次来获取其文本
  1. 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

<!-- -->

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

答案2

得分: 1

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

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

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

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

答案3

得分: 1

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

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

输出,其中 n 为 3。

  1. Rabbit
英文:

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

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

Output, where n is 3.

  1. 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:

确定