Get element from list using lambda/callback – JS equivalent of Array.prototype.find()

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

Get element from list using lambda/callback - JS equivalent of Array.prototype.find()

问题

我想通过回调从Java列表中获取一个元素(而不是索引)。

在JavaScript中,有一个名为Array.prototype.find的方法,正好可以做到这一点。
例如:

let match = [1, 2, 3, 4, 5].find((num) => num === 3);
console.log(match); // 3

在Java中是否有相当的方法?

英文:

I want to get an element (NOT the index) from a Java list using a callback.

In JavaScript, there is the Array.prototype.find method that does exactly that.
For example:

let match = [1, 2, 3, 4, 5].find((num) => num === 3);
console.log(match); // 3

Is there a Java equivalent?

答案1

得分: 5

你可以使用Java Stream API 来实现这个功能。

import java.util.List;

public class Main {
    public static void main(String[] args) {
        int x = List.of(10, 20, 30, 40, 50).stream().filter(n -> (n == 30)).findAny().orElse(-1);
        System.out.println(x);
        int y = List.of(10, 20, 30, 40, 50).stream().filter(n -> (n == 3)).findAny().orElse(-1);
        System.out.println(y);
    }
}

输出:

30
-1

根据您的需求,可以使用findAny() 或 findFirst()

英文:

You can do so using Java Stream API.

import java.util.List;

public class Main {
	public static void main(String[] args) {
		int x = List.of(10, 20, 30, 40, 50).stream().filter(n -> (n == 30)).findAny().orElse(-1);
		System.out.println(x);
		int y = List.of(10, 20, 30, 40, 50).stream().filter(n -> (n == 3)).findAny().orElse(-1);
		System.out.println(y);
	}
}

Output:

30
-1

Use findAny() or findFirst() depending on your requirement.

答案2

得分: 2

可以在Java中使用lambda函数执行相同操作:

Optional<Integer> result = Arrays.stream(array).filter(num -> (num == 3)).findAny();
result.ifPresent(num -> System.out.println(name));
英文:

You can do the same in Java using lamda function:

 Optional&lt;Integer&gt; result = Arrays.stream (array).filter(num -&gt; (num==3)).findAny();
 result.ifPresent(num -&gt; System.out.println(name));

答案3

得分: 2

你尝试过

list.stream().filter(n -> n == 3 ).findFirst().ifPresent(n -> doSomething(n))

?

英文:

Have you tried

list.stream().filter(n -&gt; n == 3 ).findFirst().ifPresent(n -&gt; doSomething(n))

?

答案4

得分: 1

如果您想确定一个数字是否存在于数组或集合中,您可以简单地使用IntStream#filter。

int value = IntStream.of(1, 2, 3, 4, 5).filter(v -> v == 3).findAny().orElse(-1);
英文:

If you're trying to determine if a number exists in an array or collection you can simply use IntStream#filter.

int value = IntStream.of(1, 2, 3, 4, 5).filter(v -&gt; v == 3).findAny().orElse(-1);

答案5

得分: 0

我在这个网站上找到了我想要的东西 - https://www.baeldung.com/find-list-element-java

基本上是这样的:

Object item = someList
  .stream()
  .filter(i -> i.getName().equalsIgnoreCase(name))
  .findAny()
  .orElse(null); 

if (item != null) {
  // ...
}
英文:

I Kinda found what i was looking for on this site - https://www.baeldung.com/find-list-element-java

Basically this:

Object item = someList
  .stream()
  .filter(i -&gt; i.getName().equalsIgnoreCase(name))
  .findAny()
  .orElse(null); 

if (item != null) {
  // ...
}

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

发表评论

匿名网友

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

确定