3

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?

3
  • 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? Commented Apr 9, 2020 at 14:42
  • Hi. i want the element, NOT the index. Commented Apr 9, 2020 at 14:48
  • 4
    Does this answer your question? Java - Find Element in Array using Condition and Lambda Commented Apr 9, 2020 at 14:51

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, yes! this is what i was looking for. Is there the possibility of null pointer exception here?
You are most welcome. I've added one more example in which the value is not present. I hope, it helps.
@ryanwaite28 No because findAny() returns an Optional of Integer. ifPresent is only called if Optional is not empty, meaning a value was found.
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));

2 Comments

Hi, i found something that helps - baeldung.com/find-list-element-java
basically this: Object item = someList .stream() .filter(i -> i.getName().equalsIgnoreCase(name)) .findAny() .orElse(null);
2

Have you tried

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

?

1 Comment

Object item = someList .stream() .filter(i -> i.getName().equalsIgnoreCase(name)) .findAny() .orElse(null);
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);

7 Comments

The op was asking about finding a number in an array...
No, OP explicitly said a List. If he is using a List he can most certainly use a Stream.
Lol well regardless your example also does not include a list
@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.
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.
|
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) {
  // ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.