Given: class Student { String course, name, city; public Student (String name, String course, String city){ this.course = course; this.name = name; this.city = city; } public String toString() { return course + ":" + name + ":" + city; } and the code fragment: List<Student> stds = Arrays.asList( new Student ("Jessy", "Java ME", "Chicago"), new Student ("Helen", "Java EE", "Houston"), new Student ("Mark", "Java ME", "Chicago")); stds.stream() .collect(Collectors.groupingBy(Student::getCourse)) .forEach(src, res) -> System.out.println(scr)); What is the result?
Click on the arrows to vote for the correct answer
A. B. C. D.B.
The code provided creates a class Student
with three fields name
, course
, and city
. The class also has a constructor that takes in values for these fields and a toString()
method that returns a string with the values of course
, name
, and city
concatenated with colons.
The code fragment creates a List
of Student
objects using the Arrays.asList
method, passing in three Student
objects as arguments.
Then, the stds
list is converted into a stream using the stream()
method. The collect
method is called on the stream with a Collectors.groupingBy
collector. This creates a Map<String, List<Student>>
where the keys are the distinct course names and the values are lists of Student
objects that have the corresponding course name.
Finally, the forEach
method is called on the resulting Map
object to print out each key (i.e., the course name) to the console.
So, the expected output should be the distinct course names printed to the console in no particular order. Based on the answer choices provided, the expected output would be option B, which is the distinct course names separated by a space:
vbnetJava EE Java ME