java lambdaj 'variable_name' cannot be resolved to a variable error
Asked Answered
H

1

6

I am new to Java 8 features and this may be a stupid question but I am stuck at this point.

I am trying to run following code in eclipse but it gives compile time error.

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;
import ch.lambdaj.Lambda;

public class LambdajTest {

    public static void main(String[] args) {

        List list = new ArrayList();
        list.add(1);
        list.add(3);
        list.add(8);
        list.add(10);
        list.add(16);

        int sum = list.stream().filter(p -> p > 10).mapToInt(p -> p).sum();
    }

}

Error is :- p cannot be resolved to a variable. I have added lambdaj 2.3.3 jar at classpath.

Kindly provide solution. Thanks in advance.

Hanuman answered 24/3, 2016 at 18:55 Comment(2)
You need to declare p as int in the lambda expressionAcquiesce
Your code does not need LambdaJ. LambdaJ is mainly a library that provides nice collection methods and is targeted to Java < 8. It has nothing to do with Java 8 Lambdas.Doralyn
R
5

The problem is that the JVM doesn't know what kind of object p is as you're using a raw collection.

Change

List list = new ArrayList();

to

List<Integer> list = new ArrayList<>();

The JVM now understands that it's streaming over a collection of Integer objects.

Receivable answered 24/3, 2016 at 19:37 Comment(1)
Makes more sense than what I thought it was.Acquiesce

© 2022 - 2024 — McMap. All rights reserved.