Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize Map from Set

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?

like image 816
IUnknown Avatar asked Apr 20 '26 14:04

IUnknown


1 Answers

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<>()));
like image 190
ernest_k Avatar answered Apr 23 '26 03:04

ernest_k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!