ArthurDn ArthurDn - 3 months ago 19
Java Question

Java 8: cast list of POJO to map - ID-POJO = map (Function<? super C, ?>) in Stream cannot be applied to (<lambda expression>, <lambda expression>)

How fix this?


map (Function)
in Stream cannot be applied
to (lambda expression>, lambda expression>)

static class C {
int id;

public int getId() {
return id;

Map<Integer, C> c = Arrays.asList(new C()).stream().collect(Collectors.toMap(c -> c.getId(), c -> c));


Try this

 Map<Integer, C> map = Arrays.asList(new C())
                          .collect(Collectors.toMap(C::getId, c -> c));

What map does in your example is change the object in the stream from one thing to another. e.g. You have a stream of C objects, and then map(c -> c.getId()) would change that to a stream of Integers. What you were looking for is the collect function which terminates the stream and allows you to "collect" it to whatever object you are trying to get to