I have a set of Student - Set<Student> students
class Student{
String Id;
String getId(){ return Id;}
.....
}
I am trying to initialize a Map<String,List<StudentResult>>
with the entries from set above:
Map<String,List<StudentResult>> studentResultMap = students.keySet().stream().collect(
Collectors.toMap(x -> x.getId(),new ArrayList<StudentResult>()));
But this wouldn't compile - how is this to be achieved?
new ArrayList<StudentResult>()
does not make a correct argument for a Function
parameter.
You need to use:
x -> new ArrayList<StudentResult>()
Side note: students.keySet()
wouldn't compile either, if students
is a Set
. You can call stream
on it directly:
students.stream().collect(Collectors.toMap(x -> x.getId(),
a -> new ArrayList<>()));
Here lies your problem:
Map<String,List<StudentResult>> studentResultMap = students
.stream().collect(Collectors.toMap(x -> x.getId(), new ArrayList<StudentResult>()));
You need to pass two functions to Collectors.toMap
, but instead, you're passing a List
instance as the second parameter
Map<String,List<StudentResult>> studentResultMap = students
.stream().collect(Collectors.toMap(x -> x.getId(), x -> new ArrayList<StudentResult>()));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.