Getting first element and returning after apply a function

Sandro Rey

I am new in Java 8, I want to make a method that gets the first element that matched and returning after apply a function

public void test() {
    List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");

    String str = features
            .stream()
            .filter(s -> "Lambdas".equals(s))
            .findFirst()
            .ifPresent(this::toLowerCase);
}

private String toLowerCase (String str) {
    return str.toLowerCase();
}

but I got an Incompatible types error.

Eran

Optional.ifPresent accepts a Consumer, and doesn't return any value. Use map:

String str =
    features.stream()
            .filter(s -> "Lambdas".equals(s))
            .findFirst()
            .map(this::toLowerCase)
            .orElse(null); // default value or orElseThrow

Or, as Holger suggested, you can move the map step into the stream pipeline:

String str =
    features.stream()
            .filter(s -> "Lambdas".equals(s))
            .map(this::toLowerCase)
            .findFirst()
            .orElse(null); // default value or orElseThrow

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting first element and returning after apply a function

From Dev

Getting string after the first _

From Dev

how to update pHead for linked list after the first element is removed in the function?

From Dev

After element to only apply to span

From Dev

function returning reference to a vector element

From Dev

Getting the first element of an empty Array

From Dev

Getting the max for the first element in an array?

From Dev

Getting first visible element with jQuery

From Java

Apply function to each element of a list

From Dev

Apply a function to the nth element of a list

From Dev

Pointer not returning after function in C

From Dev

Returning 'true' after validation function

From Dev

Returning only first element of a sub array with $elemMatch

From Dev

Returning a first element from an improper list in Erlang

From Dev

Java split() is returning an empty first element

From Dev

getElementById returning value only for first element

From Dev

returning only first element from for loop

From Dev

Java split() is returning an empty first element

From Dev

Not a Function After First Click

From Dev

tf.py_func , custom tensorflow function getting applied to only the first element in the tensor

From Dev

Apply style to the first element of a dynamic list

From Dev

Apply style to the first element of a dynamic list

From Dev

Apply specific margins to first and last element that are not siblings

From Dev

get first element after a space

From Dev

Add text after first element

From Dev

Reproduce element after first compiling

From Dev

rxjs/Observable: Running a function once after getting first stream (continuous observable)

From Java

pandas apply only returning first value when using logical indexing

From Dev

Get first item of returning function value

Related Related

  1. 1

    Getting first element and returning after apply a function

  2. 2

    Getting string after the first _

  3. 3

    how to update pHead for linked list after the first element is removed in the function?

  4. 4

    After element to only apply to span

  5. 5

    function returning reference to a vector element

  6. 6

    Getting the first element of an empty Array

  7. 7

    Getting the max for the first element in an array?

  8. 8

    Getting first visible element with jQuery

  9. 9

    Apply function to each element of a list

  10. 10

    Apply a function to the nth element of a list

  11. 11

    Pointer not returning after function in C

  12. 12

    Returning 'true' after validation function

  13. 13

    Returning only first element of a sub array with $elemMatch

  14. 14

    Returning a first element from an improper list in Erlang

  15. 15

    Java split() is returning an empty first element

  16. 16

    getElementById returning value only for first element

  17. 17

    returning only first element from for loop

  18. 18

    Java split() is returning an empty first element

  19. 19

    Not a Function After First Click

  20. 20

    tf.py_func , custom tensorflow function getting applied to only the first element in the tensor

  21. 21

    Apply style to the first element of a dynamic list

  22. 22

    Apply style to the first element of a dynamic list

  23. 23

    Apply specific margins to first and last element that are not siblings

  24. 24

    get first element after a space

  25. 25

    Add text after first element

  26. 26

    Reproduce element after first compiling

  27. 27

    rxjs/Observable: Running a function once after getting first stream (continuous observable)

  28. 28

    pandas apply only returning first value when using logical indexing

  29. 29

    Get first item of returning function value

HotTag

Archive