List de duplication problems and methods

Keywords: Java Programming

How to de duplicate the list, which is often asked in the interview, is used to check your mastery of the list data structure and related methods, and to show whether your java foundation is solid.
As we all know, set sets are characterized by no repeating elements. If the data type in the collection is the basic data type, you can directly convert the list collection to set, and the duplicate elements will be automatically removed, which is relatively simple. Here is an example:

 1 public class Test {  
 2   public static void main(String[] args) {  
 3     List list = new ArrayList();  
 4     list.add(11);  
 5     list.add(12);  
 6     list.add(13);  
 7     list.add(14);  
 8     list.add(15);  
 9     list.add(11);  
10     System.out.println(list);  
11     Set set = new HashSet();  
12     List newList = new ArrayList();  
13     set.addAll(list);  
14     newList.addAll(set);  
15     System.out.println(newList);  
16   }  
17 }  

The output result is:

 

We can see the success of de duplication.
In the interview, when asked the list de duplication question, most of the answers will be list and set, using set to automatically remove duplicate attributes to de duplication, but such answers will not score. When the type stored in the list set is the object type, we cannot simply convert the list set to the set set set. We define an object class:

 

 1 public class People {  
 2   
 3     private String name;  
 4     private String phoneNumber;  
 5   
 6     public String getName() {  
 7         return name;  
 8     }  
 9   
10     public void setName(String name) {  
11         this.name = name;  
12     }  
13   
14     public String getPhoneNumber() {  
15         return phoneNumber;  
16     }  
17   
18     public void setPhoneNumber(String phoneNumber) {  
19         this.phoneNumber = phoneNumber;  
20     }  
21   
22     public People(String name, String phoneNumber) {  
23         super();  
24         this.name = name;  
25         this.phoneNumber = phoneNumber;  
26     }  
27   
28    @Override  
29     public String toString() {  
30         return "People{" +  
31                 "name='" + name + '\'' +  
32                 ", phoneNumber='" + phoneNumber + '\'' +  
33                 '}';  
34     }  
35   
36 }  

We use the above set de duplication method to de duplicate:

 1 public static void main(String[] args) {
 2     List<People> listPeople = new ArrayList<People>();
 3     listPeople.add(new People("Zhang San", "11111"));
 4     listPeople.add(new People("Zhang San",  "22222"));
 5     listPeople.add(new People("Li Si",  "33333"));
 6     listPeople.add(new People("Zhang San",  "22222"));
 7 
 8     Set<People> setData = new HashSet<People>();
 9     setData.addAll(listPeople);
10     System.out.println("list: " + listPeople.toString());
11     System.out.println("set: " + setData.toString());
12 
13 }

The output after operation is:

 

We can see that the second Zhang San and the last Zhang San have the same information, but they have not been duplicated.
When objects are stored in the list collection, we need to override the equals() method and hashCode() method in the entity class of the object, as follows:

 1 public class People {
 2 
 3     private String name;
 4     private String phoneNumber;
 5 
 6     public String getName() {
 7         return name;
 8     }
 9 
10     public void setName(String name) {
11         this.name = name;
12     }
13 
14     public String getPhoneNumber() {
15         return phoneNumber;
16     }
17 
18     public void setPhoneNumber(String phoneNumber) {
19         this.phoneNumber = phoneNumber;
20     }
21 
22     public People(String name, String phoneNumber) {
23         super();
24         this.name = name;
25         this.phoneNumber = phoneNumber;
26 }
27 
28     @Override
29     public String toString() {
30         return "People{" +
31                 "name='" + name + '\'' +
32                 ", phoneNumber='" + phoneNumber + '\'' +
33                 '}';
34     }
35 
36     @Override
37     public boolean equals(Object arg0) {
38         // TODO Auto-generated method stub
39         People p = (People) arg0;
40         return name.equals(p.name) && phoneNumber.equals(p.phoneNumber);
41     }
42 
43     @Override
44     public int hashCode() {
45         // TODO Auto-generated method stub
46         String str = name + phoneNumber;
47         return str.hashCode();
48     }
49 
50 }

Run the above test method, and the de duplication is successful. Finally, we take out the source code of equals() method and hashCode() method in String to deepen our understanding:

equals()

 1 public boolean equals(Object anObject) {
 2         if (this == anObject) {
 3             return true;
 4         }
 5         if (anObject instanceof String) {
 6             String anotherString = (String)anObject;
 7             int n = count;
 8             if (n == anotherString.count) {
 9                 char v1[] = value;
10                 char v2[] = anotherString.value;
11                 int i = offset;
12                 int j = anotherString.offset;
13                 while (n-- != 0) {
14                     if (v1[i++] != v2[j++])
15                         return false;
16                 }
17                 return true;
18             }
19         }
20         return false;
21     }

When comparing two objects, first judge whether the two objects have the same address. If they are references to the same object, put them back to true directly. If the addresses are not the same, then prove that they are not references to the same object. Next, compare the contents of the two string pairs one by one, and return true if they are completely equal. Otherwise, false.

hashCode()

 1 public int hashCode() {
 2         int h = hash;
 3         if (h == 0 && count > 0) {
 4             int off = offset;
 5             char val[] = value;
 6             int len = count;
 7             for (int i = 0; i < len; i++) {
 8                 h = 31*h + val[off++];
 9             }
10             hash = h;
11         }
12         return h;
13     }

Official definition of hashCode():

The hashcode method returns the hash code value of the object. This method is supported to provide some advantages for hash table, for example, hash table provided by java.util.Hashtable.

The general agreement of hashCode is:

During the execution of a Java application, when calling the hashCode method multiple times on the same Object, the same integer must be returned consistently, provided that the information used in the equals comparison on the Object has not been modified. This integer does not need to be consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, calling the hashCode method on each of the two objects must produce the same integer result. The following is not necessary: if two objects are not equal according to the equals(java.lang.Object) method, calling the hashCode method on any one of the two objects will certainly generate different integer results. However, programmers should know that generating different integer results for unequal objects can improve hash table performance. In fact, the hashCode method defined by the Object class does return different integers for different objects. (this is usually done by converting the internal address of the Object to an integer, but the JavaTM programming language does not require this implementation technique.) When the equals method is overridden, it is usually necessary to override the hashCode method to maintain the general protocol of the hashCode method, which states that an equivalent Object must have an equal hash code.

Of course, there are many ways to de duplicate List. You can use the for loop or the new java8 feature stream. Here are five methods:

 1 //Build a new one. list array: 
 2 List list = new ArrayList(); 
 3 list.add(26); 
 4 list.add(39); 
 5 list.add(5); 
 6 list.add(40); 
 7 list.add(39); 
 8 list.add(25); 
 9 System.out.println(list); 
10 //Method 1:Use java8 New characteristics stream Conduct List Duplicate removal 
11 List newList = list.stream().distinct().collect(Collectors.toList()); 
12 System.out.println("java8 New characteristics stream Duplicate removal:"+newList); 
13 list.add(39); 
14 //Method two:double for Cyclic weight removal 
15 for (int i = 0; i < list.size(); i++) { 
16 for (int j = 0; j < list.size(); j++) { 
17 if(i!=j&&list.get(i)==list.get(j)) { 
18 list.remove(list.get(j)); 
19 } 
20 } 
21 } 
22 System.out.println("double for Cyclic weight removal:"+list); 
23 list.add(39); 
24 //Method three:set Set judgment de duplication,Do not disturb the order 
25 Set set1 = new HashSet(); 
26 List newList1 = new ArrayList(); 
27 for (Integer integer : list) { 
28 if(set1.add(integer)) { 
29 newList1.add(integer); 
30 } 
31 } 
32 System.out.println("set Set judgment de duplication:"+list); 
33 list.add(39); 
34 //Method four:Judge to assign another after traversal list aggregate 
35 List newList2 = new ArrayList(); 
36 for (Integer integer : list) { 
37 if(!newList2.contains(integer)){ 
38 newList2.add(integer); 
39 } 
40 } 
41 System.out.println("New assignment list Duplicate removal:"+newList2); 
42 list.add(39); 
43 //Method five:set and list Conversion to weight 
44 Set set2 = new HashSet(); 
45 List newList3 = new ArrayList(); 
46 set2.addAll(list); 
47 newList3.addAll(set2); 
48 System.out.println("set and list Conversion to weight:"+newList3);

Posted by kurky on Sun, 19 Jan 2020 01:15:26 -0800