I want to group Person objects base on gender with Collectors.toMap() function as describe below, but this does not work as I expected.
class Person{
private Integer id;
private String gender;
private String name;
public String getGender(){
return gender;
}
public Person(Integer id, String gender, String name){
this.id=id;
this.gender=gender;
this.name=name;
}
}
public static void main(String[]args){
List<Person> persons=new ArrayList<>();
persons.add(new Person(1,"Man","John"));
persons.add(new Person(2,"Man","Steve"));
persons.add(new Person(3,"Women","Linda"));
persons.add(new Person(4,"Man","Bill"));
persons.add(new Person(5,"Women","Siti"));
persons.add(new Person(6,"Man","Buzzer"));
Map<String,List<Person>> filtered=persons
.stream()
.collect(Collectors.toMap(Person:getGender:Function.identity()));
}
Error hint
incompatible types: inference variable U has incompatible bounds equality constraints: List lower bounds: Person,T#2,T#1 where U,T#1,K,T#2 are type-variables: U extends Object declared in method <T#1,K,U>toMap(Function<? super T#1,? > > extends K>,Function<? super T#1,? extends U>) T#1 extends Object declared in method <T#1,K,U>toMap(Function<? super T#1,? > extends K>,Function<? super T#1,? extends U>) K extends Object declared in method <T#1,K,U>toMap(Function<? super T#1,? > extends K>,Function<? super T#1,? extends U>) T#2 extends Object declared in method <T#2>identity()
type of <R,A>collect(Collector<? super T,A,R>) is erroneous where R,A,T are type-variables: R extends Object declared in method <R,A>collect(Collector<? super T,A,R>) A extends Object declared in method <R,A>collect(Collector<? super T,A,R>)
Can someone explain this error?