Define a person's class, save the ArrayList collection, remove the duplicate elements, define: The same name and age are the same as the repeating element.
Take weight
public class Qu {
public static void Main (string[] args) {
Sa2 a = new Sa2 ("Zhang San", 20);
SA2 A1 = new Sa2 ("Zhang San", 30);
SA2 a3 = new Sa2 ("Zhang San", 20);
arraylist<sa2> list = new arraylist<sa2> ();
List.add (a);
List.add (A1);
List.add (A3);
Arraylist<sa2> List1 = Qucong (list);
System.out.println (List1);
}
public static arraylist<sa2> Qucong (arraylist<sa2>list) {
Arraylist<sa2> list1=new arraylist<sa2> ();
Listiterator<sa2> it = List.listiterator ();
while (It.hasnext ()) {
Sa2 str = It.next ();
if (!list1.contains (str)) {
List1.add (str);
}
}
return list1;
}
}
————————————————————————————————————————————————————
public class Sa2 {
String name;
int age;
Public Sa2 (String Name,int age) {
This.name=name;
This.age=age;
}
public int hashcode () {//rewrite, change the underlying.
return 1;
}
public boolean equals (Object obj) {
if (obj instanceof Sa2) {
SA2 p = (Sa2) obj;
Return This.name.equals (p.name) && this.age = = P.age;
}
return false;
}
Public String toString () {
Return "Sa2[name" + name+ ", age=" +age+ "]";
}
}
Java ArrayList de-weight