How to specify type for lambda expression?

Jas

I tried:

Stream stream = Pattern.compile(" ").splitAsStream(sc.nextLine());
stream.forEach(item) -> {});

and got:

Compilation Error... 
 File.java uses unchecked or unsafe operations.
 Recompile with -Xlint:unchecked for details.

so I tried:

Stream stream = Pattern.compile(" ").splitAsStream(sc.nextLine());
stream.forEach((String item) -> {});

and got:

Compilation Error... 
15: error: incompatible types: incompatible parameter types in lambda expression
            stream.forEach((String item) -> {});
                           ^
 Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

How can I make this .forEach() pass compilation?

Bohemian

You've defined your Stream as a raw type, which removes all type information and (basically) uses Object as the type.

Try this:

Stream<String> stream = Pattern.compile(" ").splitAsStream(sc.nextLine());
//     ^----^ add a generic type to the declaration
stream.forEach(item -> {
    // item is know to be a String
});

Or easier, just in-line it:

Pattern.compile(" ").splitAsStream(sc.nextLine()).forEach(item -> {});

Or easier still:

Arrays.stream(sc.nextLine().split(" ")).forEach(item -> {});

Although simpler, the last version uses O(n) space because the entire input is split before the first forEach() is executed.
The other versions use O(1) space because the Pattern#splitAsStream() uses a Matcher internally to iterate through the input thus consumes the input match at a time.
Unless the input is quite large, this side effect won't make much difference.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Specify lambda expression with Generics but having a templated type

From Dev

Expression.Lambda specify ParameterExpression of sub type

From Dev

Verify the type of a lambda expression

From Dev

Linq Expression from Lambda: specify parameter explicitly

From Dev

How does compiler infer the delegate type from LAMBDA expression?

From Dev

How to use SQL IN operator into lambda expression for Guid type?

From Dev

How does compiler infer the delegate type from LAMBDA expression?

From Dev

How to specify lambda expression in JAVA when I have two same name method?

From Dev

Implicit type conversion for lambda expression

From Dev

bad return type in lambda expression

From Dev

Replace parameter type in lambda expression

From Dev

How to specify type for a variable?

From Dev

How to fix this lambda expression?

From Dev

Operator '&&' cannot be applied to operands of type 'lambda expression' and 'lambda expression

From Dev

How to specify relationship type in CSV?

From Dev

How to not specify the data type of arguments?

From Dev

How to specify data type for hashtable?

From Dev

How to specify name of configuration type?

From Dev

How to specify relationship type in CSV?

From Java

lambda expression for Consumer Interface with return type

From Dev

Why is this type inference not working with this Lambda expression scenario?

From Dev

Scala - trouble with type inference in lambda expression

From Dev

Dynamically express return type Lambda expression

From Dev

Using lambda expression to get property OR type name

From Dev

Incompatible types: bad return type in lambda expression?

From Dev

Input type changes automatically inside lambda expression

From Dev

What exactly is the type of a lambda expression with a body?

From Dev

LAMBDA error: conditional expression of type 'void' is illegal

From Dev

lambda expression for Consumer Interface with return type

Related Related

  1. 1

    Specify lambda expression with Generics but having a templated type

  2. 2

    Expression.Lambda specify ParameterExpression of sub type

  3. 3

    Verify the type of a lambda expression

  4. 4

    Linq Expression from Lambda: specify parameter explicitly

  5. 5

    How does compiler infer the delegate type from LAMBDA expression?

  6. 6

    How to use SQL IN operator into lambda expression for Guid type?

  7. 7

    How does compiler infer the delegate type from LAMBDA expression?

  8. 8

    How to specify lambda expression in JAVA when I have two same name method?

  9. 9

    Implicit type conversion for lambda expression

  10. 10

    bad return type in lambda expression

  11. 11

    Replace parameter type in lambda expression

  12. 12

    How to specify type for a variable?

  13. 13

    How to fix this lambda expression?

  14. 14

    Operator '&&' cannot be applied to operands of type 'lambda expression' and 'lambda expression

  15. 15

    How to specify relationship type in CSV?

  16. 16

    How to not specify the data type of arguments?

  17. 17

    How to specify data type for hashtable?

  18. 18

    How to specify name of configuration type?

  19. 19

    How to specify relationship type in CSV?

  20. 20

    lambda expression for Consumer Interface with return type

  21. 21

    Why is this type inference not working with this Lambda expression scenario?

  22. 22

    Scala - trouble with type inference in lambda expression

  23. 23

    Dynamically express return type Lambda expression

  24. 24

    Using lambda expression to get property OR type name

  25. 25

    Incompatible types: bad return type in lambda expression?

  26. 26

    Input type changes automatically inside lambda expression

  27. 27

    What exactly is the type of a lambda expression with a body?

  28. 28

    LAMBDA error: conditional expression of type 'void' is illegal

  29. 29

    lambda expression for Consumer Interface with return type

HotTag

Archive