Where mappedRoles = Maps.uniqueIndex
... can be seen Netbeans produces the following error:
no suitable method found for uniqueIndex(List<ReducedEntity>,<anonymous java.util.function.Function<ReducedEntity,Integer>>)
method Maps.<K#1,V#1>uniqueIndex(Iterable<V#1>,com.google.common.base.Function<? super V#1,K#1>) is not applicable
(cannot infer type-variable(s) K#1,V#1
(argument mismatch; <anonymous java.util.function.Function<ReducedEntity,Integer>> cannot be converted to com.google.common.base.Function<? super V#1,K#1>...
Demo of issue:
package com.mycompany.testground;
import com.google.common.collect.Maps;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
List<ReducedEntity> list = new ArrayList<>();
Map<Integer, ReducedEntity> mappedRoles;
mappedRoles = Maps.uniqueIndex(list, new Function<ReducedEntity, Integer>() {
@Override
public Integer apply(ReducedEntity from) {
return from.getId();
}
});
}
}
Reduced Entity Class for completeness (but probably unnecessary)
package com.mycompany.testground;
public class ReducedEntity {
protected Integer id;
protected String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Created the above after following Jons answer on this page Java: how to convert a List<?> to a Map<String,?> and am unable to yet see why this does not work as shown.