In Java, List can contain duplicate elements (hash code and equals). There are two methods to deduplicate a List:
Solution 1: You can use HashSet. The Code is as follows:
Copy codeThe Code is as follows: class Student {
Private String id;
Private String name;
Public Student (String id, String name ){
Super ();
This. id = id;
This. name = name;
}
@ Override
Public String toString (){
Return "Student [id =" + id + ", name =" + name + "]";
}
@ Override
Public int hashCode (){
Final int prime = 31;
Int result = 1;
Result = prime * result + (id = null )? 0: id. hashCode ());
Result = prime * result + (name = null )? 0: name. 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;
}
Student other = (Student) obj;
If (id = null ){
If (other. id! = Null ){
Return false;
}
} Else if (! Id. equals (other. id )){
Return false;
}
If (name = null ){
If (other. name! = Null ){
Return false;
}
} Else if (! Name. equals (other. name )){
Return false;
}
Return true;
}
}
The hashCode and equals methods must be implemented. We will see why they must be implemented later.
The specific operation code is as follows:Copy codeThe Code is as follows: private static void removeListDuplicateObject (){
List <Student> list = new ArrayList <Student> ();
For (int I = 0; I <10; I ++ ){
Student student = new Student ("id", "name ");
List. add (student );
}
System. out. println (Arrays. toString (list. toArray ()));
Set <Student> set = new HashSet <Student> ();
Set. addAll (list );
System. out. println (Arrays. toString (set. toArray ()));
List. removeAll (list );
Set. removeAll (set );
System. out. println (Arrays. toString (list. toArray ()));
System. out. println (Arrays. toString (set. toArray ()));
}
Call code:Copy codeThe Code is as follows: public static void main (String [] args ){
RemoveListDuplicateObject ();
}
If we use HashSet for deduplication, why must we overwrite the hashCode and equals methods?
The source code of the add operation of HashSet is as follows:Copy codeThe Code is as follows: public boolean add (E e ){
Return map. put (e, PRESENT) = null;
}
The HashMap operation is called. Let's look at the put Operation of HashMap:Copy codeThe Code is as follows: public V put (K key, V value ){
If (key = null)
Return putForNullKey (value );
Int hash = hash (key. hashCode ());
Int I = indexFor (hash, table. length );
For (Entry <K, V> e = table [I]; e! = Null; e = e. next ){
Object k;
If (e. hash = hash & (k = e. key) = key | key. equals (k ))){
V oldValue = e. value;
E. value = value;
E. recordAccess (this );
Return oldValue;
}
}
ModCount ++;
AddEntry (hash, key, value, I );
Return null;
}
Note that:Copy codeThe Code is as follows: if (e. hash = hash & (k = e. key) = key | key. equals (k ))){
......
}
That is to say, the hash code is equal and equals (= ).
Complexity: Just traverse one side, O (n)
Solution 2: directly traverse the List and perform the contains and add operations.
The Code is as follows:Copy codeThe Code is as follows: private static void removeListDuplicateObjectByList (){
List <Student> list = new ArrayList <Student> ();
For (int I = 0; I <10; I ++ ){
Student student = new Student ("id", "name ");
List. add (student );
}
System. out. println (Arrays. toString (list. toArray ()));
List <Student> listUniq = new ArrayList <Student> ();
For (Student student: list ){
If (! ListUniq. contains (student )){
ListUniq. add (student );
}
}
System. out. println (Arrays. toString (listUniq. toArray ()));
List. removeAll (list );
ListUniq. removeAll (listUniq );
System. out. println (Arrays. toString (list. toArray ()));
System. out. println (Arrays. toString (listUniq. toArray ()));
}
Others are the same as above.
Complexity:
While traversing and calling the contains method, we can view the source code as follows:Copy codeThe Code is as follows: public boolean contains (Object o ){
Return indexOf (o)> = 0;
}
Public int indexOf (Object o ){
If (o = null ){
For (int I = 0; I <size; I ++)
If (elementData [I] = null)
Return I;
} Else {
For (int I = 0; I <size; I ++)
If (o. equals (elementData [I])
Return I;
}
Return-1;
}
We can see that another traversal operation is performed on the new list. That is, the complexity of 1 + 2 +... + n is O (n * n)
Conclusion:
Solution 1 is highly efficient, that is, the HashSet method is used for deduplication.