public class TestMain {
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();
students.add(new Student("1", "zhangsan", "20"));
students.add(new Student("1", "zhangsan", "20"));
students.add(new Student("2", "zhangsan2", "22"));
students.add(new Student("2", "zhangsan2", "22"));
students.add(new Student("3", "zhangsan3", "23"));
List<Student> list = getNewList(students);
for(Student stu:list){
System.out.println(stu.getId()+" name:"+stu.getName());
}
}
public static List getNewList(List students){
List<Student> list = new ArrayList<Student>();
//set集合保存的是引用不同地址的对象
Set<Student> ts = new HashSet<Student>();
ts.addAll(students);
for(Student s:ts){
list.add(s);
}
return list;
}
}
public class Student {
private String id;private String name;
private String age;
public Student(String id, String name, String age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
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 String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public boolean equals(Object obj) {
Student s=(Student)obj;
return id.equals(s.id) && name.equals(s.name) && age.equals(s.age);
}
@Override
public int hashCode() {
String in = id + name +age;
return in.hashCode();
}
}