public class Test9 {
static class Student {
String stud_id;
String stud_name;
String stud_location;
public Student(String stud_id, String stud_name, String stud_location) {
super();
this.stud_id = stud_id;
this.stud_name = stud_name;
this.stud_location = stud_location;
}
public String getStud_id() {
return stud_id;
}
public void setStud_id(String stud_id) {
this.stud_id = stud_id;
}
public String getStud_name() {
return stud_name;
}
public void setStud_name(String stud_name) {
this.stud_name = stud_name;
}
public String getStud_location() {
return stud_location;
}
public void setStud_location(String stud_location) {
this.stud_location = stud_location;
}
@Override
public String toString() {
return " [stud_id=" + stud_id + ", stud_name=" + stud_name + "]";
}
}
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student("1726", "John Easton", "Lancaster"));
list.add(new Student("4321", "Max Carrados", "London"));
list.add(new Student("2234", "Andrew Lewis", "Lancaster"));
list.add(new Student("5223", "Michael Benson", "Leeds"));
list.add(new Student("5225", "Sanath Jayasuriya", "Leeds"));
list.add(new Student("7765", "Samuael Vatican", "California"));
list.add(new Student("3442", "Mark Farley", "Ladykirk"));
list.add(new Student("3443", "Alex Stuart", "Ladykirk"));
list.add(new Student("4321", "Michael Stuart", "California"));
Map<String, List<Student>> map1 =
list
.stream()
.sorted(Comparator.comparing(Student::getStud_id)
.thenComparing(Student::getStud_name)
.thenComparing(Student::getStud_location)
)
.collect(Collectors.groupingBy(
ch -> ch.stud_location
));
System.out.println(map1);
/*
Output :
{Ladykirk=[ [stud_id=3442, stud_name=Mark Farley],
[stud_id=3443, stud_name=Alex Stuart]],
Leeds=[ [stud_id=5223, stud_name=Michael Benson],
[stud_id=5225, stud_name=Sanath Jayasuriya]],
London=[ [stud_id=4321, stud_name=Max Carrados]],
Lancaster=[ [stud_id=1726, stud_name=John Easton],
[stud_id=2234, stud_name=Andrew Lewis]],
California=[ [stud_id=4321, stud_name=Michael Stuart],
[stud_id=7765, stud_name=Samuael Vatican]]}
*/
}// main
}