package mypack;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Test
{
public static void main(String[] args) throws ParseException
{
Student s1 = new Student("1", "S1", 10);
Student s2 = new Student("1", "S1", 10);
Student s3 = new Student("1", "S1", 10);
Student s4 = new Student("2", "S2", 20);
Student s5 = new Student("2", "S2", 20);
Student s6 = new Student("2", "S2", 20);
Student s7 = new Student("3", "S3", 30);
Student s8 = new Student("3", "S3", 30);
Student s9 = new Student("4", "S4", 40);
Student s10 = new Student("4", "S4", 40);
List<Student> sList = new ArrayList<Student>();
sList.add(s1);
sList.add(s2);
sList.add(s3);
sList.add(s4);
sList.add(s5);
sList.add(s6);
sList.add(s7);
sList.add(s8);
sList.add(s9);
sList.add(s10);
Map<ParentStudent, List<Student>> sMap = sList.stream().collect(Collectors.groupingBy(student -> new ParentStudent(student.getId())));
sMap.forEach((key, value) -> {
System.out.print("id:" + key.getId() + "====");
for(Student s : value)
{
System.out.print("name:" + s.getName() + ",");
}
System.out.println("");
});
}
}
class ParentStudent extends SupperStudent
{
String id;
public ParentStudent(String sid)
{
this.id = sid;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
ParentStudent other = (ParentStudent)obj;
if(id == null)
{
if(other.id != null)
return false;
}
else if(!id.equals(other.id))
return false;
return true;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
}
class SupperStudent
{
private String sex;
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
}
class Student
{
String id;
String name;
int age;
public Student(String sid)
{
this.id = sid;
}
public Student(String sid, String sname, int sage)
{
this.id = sid;
this.name = sname;
this.age = sage;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}