前言
因為在面試的時候碰到幾次list的去重和排序,覺著有必要給大家總結一下具體的方法,分享出來供大家學習參考,話不多說了,來一起看看下面介紹的一種做法:
一、list去重
1.1 實體類Student
List<Student>
容量10k以上,要求去重復。這里Student的重復標準是屬性相同,因此需要重寫equals和hashcode方法,不知道有幾個可以手寫出來。
student的equals方法:
1
2
3
4
5
6
7
8
|
public void equals(Object o){ if ( this == o) retun true ; if (!(o instanceof Student)) return false ; Student stu = (Studend)o; if (id!=stu.id) return false ; if (age!=stu.age) return false ; return name!= null ? name.equals(stu.name) : stu.name == null ; } |
這里只要記住宗旨是比較Student的屬性即可,如果屬性相同則相等。先考慮地址相等,然后類型匹配instanceof。接下來是各種屬性,int屬性直接雙等號比較,String類型需要判斷是否為null,如果是null則都是null返回true,如果不是null則比較equals。
student的hashcode方法:
1
2
3
4
5
6
|
public int hashCode(){ int result = id; reuslt = 31 *id +(name!= null ?name.hashCode(): 0 ); reuslt = 31 *age; return reuslt; } |
hashCode是為了hash表計算做輔助,方便快速查找。因此hash算法的結果要盡量的散列。這里用到31,這個31在別的博客中看到的原因是這樣的: obj*31==obj<<5-obj
.左移5位相當乘以2的5次方,就是32.null的hashCode為空。
通過equals和hashCode的實現可以發現,如果equals為true,則所有屬性相同,而屬性相同則計算出的hashCode必然相同。然而hashCode相同,屬性未必一樣,即equals不一定為真。
關于hashCode的價值體現并不在這里,而在于HashMap的實現。HashMap內部是通過鏈表數組的hash結構來實現的,這里就要用到hashcode。
下面是完整的Student代碼:
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
|
package com.test.arithmetic.listequals; /** * 這里id,name,age相同則Student相同, * 若有其他相同 * Created by Administrator on 2016/3/29. */ public class Student { int id; String name; int age; public Student( int id, String name, int age) { this .id = id; this .name = name; this .age = age; } @Override public boolean equals(Object o) { if ( this == o) return true ; if (!(o instanceof Student)) return false ; Student student = (Student) o; if (id != student.id) return false ; if (age != student.age) return false ; return name != null ? name.equals(student.name) : student.name == null ; } @Override public int hashCode() { int result = id; result = 31 * result + (name != null ? name.hashCode() : 0 ); result = 31 * result + age; return result; } } |
1.2通過HashSet去重
如果你覺得自己可以hold住一個完善的hash算法就可以自己去實現它。這里采用jdk自帶的HashSet來完成重復獲取。
先放代碼:
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
|
package com.test.arithmetic.listequals; import org.junit.Assert; import java.util.*; /** * 取出list中重復的Student對象 * Created by Administrator on 2016/3/29. */ public class ObtainListEquals { public static void main(String[] args){ //原始數據 List<Student> list = new ArrayList<>(); //重復數據 List<Student> list2 = new ArrayList<>(); //填充 for ( int i = 0 ; i < 10 ; i++) { list.add( new Student(i, "_" +i, 18 +i)); Random random = new Random(); if (random.nextBoolean()){ list.add( new Student(i, "_" +i, 18 +i)); } } //使用hashset去重復,set為重復的集合,可以通過new ArrayList(set)轉換成list HashSet<Student> set = new HashSet<>(); for (Student student : list) { boolean add = set.add(student); if (!add){ list2.add(student); } } //比較 Assert.assertEquals(list.size(),list2.size()+set.size()); } } |
去重的原理和簡單,無論你僅僅是想把重復的丟掉,或者將重復的取出來。這里去掉的是第二次遇到的對象,取出的也是第二次遇到的對象。HashSet中的add方法會返回一個Boolean值,如果插入的值已經存在,則直接返回false。關于hashset的源碼放到以后研究。大概的說,是通過HashMap的key來實現的,而HashMap在1.8中改動很大,據說是用紅黑樹實現的,提高了get的時間復雜度。
二、list對象排序
同樣list中存放的是Student對象,我需要一個規則來排序。這個排序的規則這里定義為id的比較大小。參考:java中list排序
2.1 Student對象實現Comparable接口
Comparable接口提供一個比較的compareTo(Object o)
方法,通過返回值>0,=0,<0比較大小。這里由于僅僅把id當做比較大小的方法,直接用id做減法,如果是要比較對象,建議套用this.property.compareTo(o.property)
.
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
|
package com.test.arithmetic.listequals; /** * 這里id,name,age相同則Student相同, * 若有其他相同 * Created by Administrator on 2016/3/29. */ public class Student implements Comparable<Student>{ int id; String name; int age; public Student( int id, String name, int age) { this .id = id; this .name = name; this .age = age; } @Override public boolean equals(Object o) { if ( this == o) return true ; if (!(o instanceof Student)) return false ; Student student = (Student) o; if (id != student.id) return false ; if (age != student.age) return false ; return name != null ? name.equals(student.name) : student.name == null ; } @Override public int hashCode() { int result = id; result = 31 * result + (name != null ? name.hashCode() : 0 ); result = 31 * result + age; return result; } @Override public int compareTo(Student o) { return this .id-o.id; } } |
通過Collections.sort(list)排序:
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
|
package com.test.arithmetic.list.sort; import com.test.arithmetic.list.Student; import org.junit.Before; import org.junit.Test; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * 對list中對象排序 * Created by Administrator on 2016/3/29. */ public class SortList { List<Student> list; @Before public void setUp(){ list = new ArrayList<>(); for ( int i = 0 ; i < 10 ; i++) { int v = ( int )(Math.random() * 100 ); list.add( new Student(v, "_" +v, 18 +v)); } System.out.println( "原list:" +list); } //方法一,對象實現Comparable接口 @Test public void byImplements(){ Collections.sort(list); System.out.println( "排序后:" +list); } } |
2.2 重載sort方法,傳入一個比較器
Student類還是未實現Comparable接口之前的:
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
50
51
52
53
54
55
56
|
package com.test.arithmetic.list; /** * 這里id,name,age相同則Student相同, * 若有其他相同 * Created by Administrator on 2016/3/29. */ public class Student{ int id; String name; int age; public Student( int id, String name, int age) { this .id = id; this .name = name; this .age = age; } public int getId() { return id; } public Student( int id) { this .id = id; } @Override public boolean equals(Object o) { if ( this == o) return true ; if (!(o instanceof Student)) return false ; Student student = (Student) o; if (id != student.id) return false ; if (age != student.age) return false ; return name != null ? name.equals(student.name) : student.name == null ; } @Override public int hashCode() { int result = id; result = 31 * result + (name != null ? name.hashCode() : 0 ); result = 31 * result + age; return result; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\ '' + ", age=" + age + '}' ; } } |
在排序的代碼出添加排序規則:
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
|
package com.test.arithmetic.list.sort; import com.test.arithmetic.list.Student; import org.junit.Before; import org.junit.Test; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * 對list中對象排序 * Created by Administrator on 2016/3/29. */ public class SortList { List<Student> list; @Before public void setUp(){ list = new ArrayList<>(); for ( int i = 0 ; i < 10 ; i++) { int v = ( int )(Math.random() * 100 ); list.add( new Student(v, "_" +v, 18 +v)); } System.out.println( "原list:" +list); } //方法一,對象實現Comparable接口 @Test public void byImplements(){ // Collections.sort(list); System.out.println( "排序后:" +list); } /*方法二,添加比較器*/ @Test public void byOverideCompare(){ Collections.sort(list, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.getId()-o2.getId(); } }); System.out.println(list); } } |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。