list中數據的去重,通常使用將list轉換為set,簡單直接,因為set集合的特點就是沒有重復的元素。需要考慮一下兩種情況:
1.list集合中的數據類型是基本數據類型
可以直接將list集合轉換成set,就會自動去除重復的元素。
如下示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class test { public static void main(string[] args) { list list = new arraylist(); list.add( 11 ); list.add( 12 ); list.add( 13 ); list.add( 14 ); list.add( 15 ); list.add( 11 ); system.out.println(list); set set = new hashset(); list newlist = new arraylist(); set.addall(list); newlist.addall(set); system.out.println(newlist); } } |
2.list集合中存儲的數據類型是對象類型
需要在對象的實體類中去重寫equals()方法和hashcode()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
public class people { private string name; private string phonenumber; public string getname() { return name; } public void setname(string name) { this .name = name; } public string getphonenumber() { return phonenumber; } public void setphonenumber(string phonenumber) { this .phonenumber = phonenumber; } public people(string name, string phonenumber) { super (); this .name = name; this .phonenumber = phonenumber; } @override public string tostring() { return "people{" + "name='" + name + ' '' + ", phonenumber='" + phonenumber + ' '' + '}' ; } @override public boolean equals(object o) { people p = (people) o; return name.equals(p.name) && phonenumber.equals(p.phonenumber); } @override public int hashcode() { string str = name + phonenumber; return str.hashcode(); } } public static void main(string[] args) { list<people> listpeople = new arraylist<people>(); listpeople.add( new people( "張三" , "11111" )); listpeople.add( new people( "張三" , "22222" )); listpeople.add( new people( "李四" , "33333" )); listpeople.add( new people( "張三" , "22222" )); set<people> setdata = new hashset<people>(); setdata.addall(listpeople); system.out.println( "list:" + listpeople.tostring()); system.out.println( "set:" + setdata.tostring()); } |
最后,我們拿出string中的equals()方法和hashcode()方法源碼來加深認識:
equals()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public boolean equals(object anobject) { if ( this == anobject) { return true ; } if (anobject instanceof string) { string anotherstring = (string)anobject; int n = count; if (n == anotherstring.count) { char v1[] = value; char v2[] = anotherstring.value; int i = offset; int j = anotherstring.offset; while (n-- != 0 ) { if (v1[i++] != v2[j++]) return false ; } return true ; } } return false ; } |
比較兩個對象時,首先先去判斷兩個對象是否具有相同的地址,如果是同一個對象的引用,則直接放回true;如果地址不一樣,則證明不是引用同一個對象,接下來就是挨個去比較兩個字符串對象的內容是否一致,完全相等返回true,否則false。
hashcode()
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public int hashcode() { int h = hash; if (h == 0 && count > 0 ) { int off = offset; char val[] = value; int len = count; for ( int i = 0 ; i < len; i++) { h = 31 *h + val[off++]; } hash = h; } return h; } |
hashcode()官方定義:
hashcode方法返回該對象的哈希碼值。支持該方法是為哈希表提供一些優點,例如,java.util.hashtable 提供的哈希表。
hashcode 的常規協定是:
在 java 應用程序執行期間,在同一對象上多次調用 hashcode 方法時,必須一致地返回相同的整數,前提是對象上 equals 比較中所用的信息沒有被修改。從某一應用程序的一次執行到同一應用程序的另一次執行,該整數無需保持一致。
如果根據 equals(object) 方法,兩個對象是相等的,那么在兩個對象中的每個對象上調用 hashcode 方法都必須生成相同的整數結果。
以下情況不是必需的:如果根據 equals(java.lang.object) 方法,兩個對象不相等,那么在兩個對象中的任一對象上調用 hashcode 方法必定會生成不同的整數結果。但是,程序員應該知道,為不相等的對象生成不同整數結果可以提高哈希表的性能。
實際上,由 object 類定義的 hashcode 方法確實會針對不同的對象返回不同的整數。(這一般是通過將該對象的內部地址轉換成一個整數來實現的,但是 javatm 編程語言不需要這種實現技巧。)
當equals方法被重寫時,通常有必要重寫 hashcode 方法,以維護 hashcode 方法的常規協定,該協定聲明相等對象必須具有相等的哈希碼。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/ym01213/article/details/85333529