39

In short, I have this code, and I'd like to get an specific element of the array using a condition and lambda. The code would be something like this:

Preset[] presets = presetDALC.getList();
Preset preset = Arrays.stream(presets).select(x -> x.getName().equals("MyString"));

But obviously this does not work. In C# would be something similar but in Java, how do I do this?

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
Joe Almore
  • 4,036
  • 9
  • 52
  • 77

4 Answers4

69

You can do it like this,

Optional<Preset> optional = Arrays.stream(presets)
                                   .filter(x -> "MyString".equals(x.getName()))
                                   .findFirst();

if(optional.isPresent()) {//Check whether optional has element you are looking for
    Preset p = optional.get();//get it from optional
}

You can read more about Optional here.

Moazzam Khan
  • 3,130
  • 2
  • 20
  • 35
akash
  • 22,664
  • 11
  • 59
  • 87
  • Using `isPresent()` and `.get()` is discouraged as it kills the purpose of using optional. – Moazzam Khan Aug 18 '19 at 11:26
  • 4
    Not directly. The main reason to use Optional is to force a check (to trigger a bug as early as possibly). Using get() is okay if you can prove that it holds, which he does. I agree though that in general its better to use the alternatives. But there are many situations in which you can not directly name a default or want to throw an exception, instead you may want to have the error handling in the `if-else` of `isPresent()` and then use `get()`. – Zabuzard Aug 18 '19 at 11:41
  • 1
    Your answer was helpful for me. I wanted the same exact thing. Thanks. – Harisha K P Aug 04 '22 at 14:48
33

Like this:

Optional<Preset> preset = Arrays
        .stream(presets)
        .filter(x -> x.getName().equals("MyString"))
        .findFirst();

This will return an Optional which might or might not contain a value. If you want to get rid of the Optional altogether:

Preset preset = Arrays
        .stream(presets)
        .filter(x -> x.getName().equals("MyString"))
        .findFirst()
        .orElse(null);

The filter() operation is an intermediate operation which returns a lazy stream, so there's no need to worry about the entire array being filtered even after a match is encountered.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
12

Do you want first matching, or all matching?

String[] presets = {"A", "B", "C", "D", "CA"};

// Find all matching
List<String> resultList = Arrays.stream(presets)
                                .filter(x -> x.startsWith("C"))
                                .collect(Collectors.toList());
System.out.println(resultList);

// Find first matching
String firstResult = Arrays.stream(presets)
                           .filter(x -> x.startsWith("C"))
                           .findFirst()
                           .orElse(null);
System.out.println(firstResult);

Output

[C, CA]
C
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

One-liner since Java 8:

Preset found = Stream.of(presets)
    .filter(p -> p.getName().equals("MyString"))
    .findFirst().orElseThrow();

Avoid declaring Optional<>, that returned from findFirst as a variable or a parameter. It is designed for return types / "one-liner" styles.

epox
  • 9,236
  • 1
  • 55
  • 38
  • Is there a one-liner that doesn't throw an exception in the case of a missing element? E.g. something you could toss into a conditional so you can then add the missing element if needed. – Fopedush Dec 31 '20 at 23:00
  • 1
    @Fopedush, absolutely: `...findFirst().orElse(MY_EMPTY_OBJ);`. See: https://docs.oracle.com/javase/9/docs/api/java/util/Optional.html#orElse-T- – epox Jan 04 '21 at 18:18