Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java lambdaj 'variable_name' cannot be resolved to a variable error

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.

like image 570
s developer Avatar asked Mar 24 '16 18:03

s developer


1 Answers

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.

like image 80
tddmonkey Avatar answered Sep 24 '22 14:09

tddmonkey