0

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?

ryanwaite28
  • 1,385
  • 2
  • 18
  • 38
  • 1
    Are you trying to find the index or trying to find the element 3? If you're looking for 3 why would you return 3? Wouldn't you just want to know if it exists or not? – Jason Apr 09 '20 at 14:42
  • Hi. i want the element, NOT the index. – ryanwaite28 Apr 09 '20 at 14:48
  • 4
    Does this answer your question? [Java - Find Element in Array using Condition and Lambda](https://stackoverflow.com/questions/32262059/java-find-element-in-array-using-condition-and-lambda) – Savior Apr 09 '20 at 14:51

5 Answers5

3

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.

Arvind Kumar Avinash
  • 62,771
  • 5
  • 54
  • 92
2

You can do the same in Java using lamda function:

 Optional<Integer> result = Arrays.stream (array).filter(num -> (num==3)).findAny();
 result.ifPresent(num -> System.out.println(name));
Tarun
  • 3,063
  • 3
  • 26
  • 43
2

Have you tried

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

?

domin4815
  • 36
  • 2
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 -> v == 3).findAny().orElse(-1);
Jason
  • 4,936
  • 2
  • 11
  • 21
  • The op was asking about finding a number in an array... – RobOhRob Apr 09 '20 at 14:46
  • 1
    No, OP explicitly said a List. If he is using a List he can most certainly use a Stream. – Jason Apr 09 '20 at 14:47
  • Lol well regardless your example also does not include a list – RobOhRob Apr 09 '20 at 14:48
  • 1
    @RobOhRob Because unless you had a predefined list you would almost never create a list so that you can filter through it when you can just use a Stream. Why would you use Arrays.asList(1, 2, 3).stream() when you can just use Stream.of(1, 2, 3) or IntStream.of(1, 2, 3) in this case. You would literally be creating a List, then a Stream. – Jason Apr 09 '20 at 14:50
  • Lmao so you're saying that IntStream implements the list interface? – RobOhRob Apr 09 '20 at 14:57
  • 1
    Guys, please. No need for hostility; thanks for trying to help. I'm new to Java, i'm coming from JavaScript background. In JS, there is the Array.prototype.find method AND Array.prototype.findIndex method (i find JS to be much more flexible as a language). Not sure if there was a Java Equivalent. – ryanwaite28 Apr 09 '20 at 14:59
  • @Jason constructive criticism can be helpful Jason. I was simply stating that this answer did not show how to filter a list... I personally don't consider an IntStream to be a list, because it doesn't implement the list interface and I'm not aware of any other lists in java – RobOhRob Apr 09 '20 at 15:10
0

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 -> i.getName().equalsIgnoreCase(name))
  .findAny()
  .orElse(null); 

if (item != null) {
  // ...
}
ryanwaite28
  • 1,385
  • 2
  • 18
  • 38