JAVA學習篇14——集合詳解
概述:
- List , Set, Map都是介面,前兩個繼承至Collection介面,Map為獨立介面
- Set下有HashSet,LinkedHashSet,TreeSet
- List下有ArrayList,Vector,LinkedList
- Map下有Hashtable,LinkedHashMap,HashMap,TreeMap
- Collection介面下還有個Queue介面,有PriorityQueue類
注意:
-
Queue介面與List、Set同一級別,都是繼承了Collection介面。 看圖你會發現,LinkedList既可以實現Queue介面,也可以實現List介面.只不過呢, LinkedList實現了Queue介面。Queue介面窄化了對LinkedList的方法的訪問許可權(即在方法中的引數型別如果是Queue時,就完全只能訪問Queue介面所定義的方法 了,而不能直接訪問 LinkedList的非Queue的方法),以使得只有恰當的方法才可以使用。
-
SortedSet是個介面,它裡面的(只有TreeSet這一個實現可用)中的元素一定是有序的。
總結:
Connection介面:
— List 有序,可重複
- ArrayList優點: 底層資料結構是陣列,查詢快,增刪慢。缺點: 執行緒不安全,效率高
- Vector優點: 底層資料結構是陣列,查詢快,增刪慢。缺點: 執行緒安全,效率低
- LinkedList優點: 底層資料結構是連結串列,查詢慢,增刪快。缺點: 執行緒不安全,效率高
—Set 無序,唯一
-
HashSet 底層資料結構是雜湊表。(無序,唯一) 如何來保證元素唯一性? 1.依賴兩個方法:hashCode()和equals()
-
LinkedHashSet 底層資料結構是連結串列和雜湊表。(FIFO插入有序,唯一) 1.由連結串列保證元素有序 2.由雜湊表保證元素唯一
-
TreeSet 底層資料結構是紅黑樹。(唯一,有序) 1. 如何保證元素排序的呢? 自然排序 比較器排序 2.如何保證元素唯一性的呢? 根據比較的返回值是否是0來決定
針對Collection集合我們到底使用誰呢?(掌握)
唯一嗎?
是:Set
排序嗎?
是:TreeSet或LinkedHashSet 否:HashSet 如果你知道是Set,但是不知道是哪個Set,就用HashSet。
否:List
要安全嗎?
是:Vector 否:ArrayList或者LinkedList
查詢多:ArrayList 增刪多:LinkedList 如果你知道是List,但是不知道是哪個List,就用ArrayList。
如果你知道是Collection集合,但是不知道使用誰,就用ArrayList。 如果你知道用集合,就用ArrayList。
說完了Collection,來簡單說一下Map.
Map介面:
上圖:
Map介面有三個比較重要的實現類,分別是HashMap、TreeMap和HashTable。
- TreeMap是有序的,HashMap和HashTable是無序的。
- Hashtable的方法是同步的,HashMap的方法不是同步的。這是兩者最主要的區別。
這就意味著:
- Hashtable是執行緒安全的,HashMap不是執行緒安全的。
- HashMap效率較高,Hashtable效率較低。 如果對同步性或與遺留程式碼的相容性沒有任何要求,建議使用HashMap。 檢視Hashtable的原始碼就可以發現,除建構函式外,Hashtable的所有 public 方法宣告中都有 synchronized關鍵字,而HashMap的原始碼中則沒有。
- Hashtable不允許null值,HashMap允許null值(key和value都允許)
- 父類不同:Hashtable的父類是Dictionary,HashMap的父類是AbstractMap
重點問題重點分析:
(一).TreeSet, LinkedHashSet and HashSet 的區別
1. 介紹
- TreeSet, LinkedHashSet and HashSet 在java中都是實現Set的資料結構
- TreeSet的主要功能用於排序
- LinkedHashSet的主要功能用於保證FIFO即有序的集合(先進先出)
- HashSet只是通用的儲存資料的集合
2. 相同點
- Duplicates elements: 因為三者都實現Set interface,所以三者都不包含duplicate elements
- Thread safety: 三者都不是執行緒安全的,如果要使用執行緒安全可以Collections.synchronizedSet()
3. 不同點
- Performance and Speed: HashSet插入資料最快,其次LinkHashSet,最慢的是TreeSet因為內部實現排序
- Ordering: HashSet不保證有序,LinkHashSet保證FIFO即按插入順序排序,TreeSet安裝內部實現排序,也可以自定義排序規則
- null:HashSet和LinkHashSet允許存在null資料,但是TreeSet中插入null資料時會報NullPointerException
4. 程式碼比較
public static void main(String args[]) {
HashSet<String> hashSet = new HashSet<>();
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
TreeSet<String> treeSet = new TreeSet<>();
<span class="token keyword">for</span> <span class="token punctuation">(</span>String data <span class="token operator">:</span> Arrays<span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span><span class="token string">"B"</span><span class="token punctuation">,</span> <span class="token string">"E"</span><span class="token punctuation">,</span> <span class="token string">"D"</span><span class="token punctuation">,</span> <span class="token string">"C"</span><span class="token punctuation">,</span> <span class="token string">"A"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> hashSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span> linkedHashSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span> treeSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//不保證有序</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Ordering in HashSet :"</span> <span class="token operator">+</span> hashSet<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//FIFO保證安裝插入順序排序</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Order of element in LinkedHashSet :"</span> <span class="token operator">+</span> linkedHashSet<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//內部實現排序</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Order of objects in TreeSet :"</span> <span class="token operator">+</span> treeSet<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
執行結果: Ordering in HashSet :[A, B, C, D, E] (無順序) Order of element in LinkedHashSet :[B, E, D, C, A] (FIFO插入有序) Order of objects in TreeSet :[A, B, C, D, E] (排序)
(二).TreeSet的兩種排序方式比較
1.排序的引入(以基本資料型別的排序為例)
由於TreeSet可以實現對元素按照某種規則進行排序,例如下面的例子
public class MyClass {
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 建立集合物件</span> <span class="token comment">// 自然順序進行排序</span> TreeSet<span class="token generics function"><span class="token punctuation"><</span>Integer<span class="token punctuation">></span></span> ts <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics function"><span class="token punctuation"><</span>Integer<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 建立元素並新增</span> <span class="token comment">// 20,18,23,22,17,24,19,18,24</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">23</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">22</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">17</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">24</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">19</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">24</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 遍歷</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>Integer i <span class="token operator">:</span> ts<span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
}
- 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
執行結果: 17 18 19 20 22 23 24
2.如果是引用資料型別呢,比如自定義物件,又該如何排序呢?
測試類:
public class MyClass {
public static void main(String[] args) {
TreeSet<Student> ts=new TreeSet<Student>();
//建立元素物件
Student s1=new Student("zhangsan",20);
Student s2=new Student("lis",22);
Student s3=new Student("wangwu",24);
Student s4=new Student("chenliu",26);
Student s5=new Student("zhangsan",22);
Student s6=new Student("qianqi",24);
<span class="token comment">//將元素物件新增到集合物件中</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>s3<span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>s4<span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>s5<span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>s6<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//遍歷</span> <span class="token keyword">for</span><span class="token punctuation">(</span>Student s<span class="token operator">:</span>ts<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"-----------"</span><span class="token operator">+</span>s<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
}
- 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
Student.java:
public class Student {
private String name;
private int age;
<span class="token keyword">public</span> <span class="token function">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// TODO Auto-generated constructor stub</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token function">Student</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span> <span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span>String name<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> age<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 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
結果報錯:
原因分析: 由於不知道該安照那一中排序方式排序,所以會報錯。 解決方法: 1.自然排序 2.比較器排序
(1).自然排序
自然排序要進行一下操作: 1.Student類中實現 Comparable介面 2.重寫Comparable介面中的Compareto方法
compareTo(T o) 比較此物件與指定物件的順序。
- 1
public class Student implements Comparable<Student>{
private String name;
private int age;
<span class="token keyword">public</span> <span class="token function">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// TODO Auto-generated constructor stub</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token function">Student</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span> <span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span>String name<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> age<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">compareTo</span><span class="token punctuation">(</span>Student s<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//return -1; //-1表示放在紅黑樹的左邊,即逆序輸出</span> <span class="token comment">//return 1; //1表示放在紅黑樹的右邊,即順序輸出</span> <span class="token comment">//return o; //表示元素相同,僅存放第一個元素</span> <span class="token comment">//主要條件 姓名的長度,如果姓名長度小的就放在左子樹,否則放在右子樹</span> <span class="token keyword">int</span> num<span class="token operator">=</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span>s<span class="token punctuation">.</span>name<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//姓名的長度相同,不代表內容相同,如果按字典順序此 String 物件位於引數字串之前,則比較結果為一個負整數。</span> <span class="token comment">//如果按字典順序此 String 物件位於引數字串之後,則比較結果為一個正整數。</span> <span class="token comment">//如果這兩個字串相等,則結果為 0</span> <span class="token keyword">int</span> num1<span class="token operator">=</span>num<span class="token operator">==</span><span class="token number">0</span><span class="token operator">?</span><span class="token keyword">this</span><span class="token punctuation">.</span>name<span class="token punctuation">.</span><span class="token function">compareTo</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token operator">:</span>num<span class="token punctuation">;</span> <span class="token comment">//姓名的長度和內容相同,不代表年齡相同,所以還要判斷年齡</span> <span class="token keyword">int</span> num2<span class="token operator">=</span>num1<span class="token operator">==</span><span class="token number">0</span><span class="token operator">?</span><span class="token keyword">this</span><span class="token punctuation">.</span>age<span class="token operator">-</span>s<span class="token punctuation">.</span>age<span class="token operator">:</span>num1<span class="token punctuation">;</span> <span class="token keyword">return</span> num2<span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 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
執行結果:
lis-----------22 qianqi-----------24 wangwu-----------24 chenliu-----------26 zhangsan-----------20 zhangsan-----------22
(2).比較器排序
比較器排序步驟: 1.單獨建立一個比較類,這裡以MyComparator為例,並且要讓其繼承Comparator介面 2.重寫Comparator介面中的Compare方法
compare(T o1,T o2) 比較用來排序的兩個引數。
- 1
3.在主類中使用下面的 構造方法
TreeSet(Comparator<? superE> comparator)
構造一個新的空 TreeSet,它根據指定比較器進行排序。
- 1
- 2
測試類:
public class MyClass {
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//建立集合物件</span> <span class="token comment">//TreeSet(Comparator<? super E> comparator) 構造一個新的空 TreeSet,它根據指定比較器進行排序。</span> TreeSet<span class="token generics function"><span class="token punctuation"><</span>Student<span class="token punctuation">></span></span> ts<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics function"><span class="token punctuation"><</span>Student<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MyComparator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//建立元素物件</span> Student s1<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">"zhangsan"</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Student s2<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">"lis"</span><span class="token punctuation">,</span><span class="token number">22</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Student s3<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">"wangwu"</span><span class="token punctuation">,</span><span class="token number">24</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Student s4<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">"chenliu"</span><span class="token punctuation">,</span><span class="token number">26</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Student s5<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">"zhangsan"</span><span class="token punctuation">,</span><span class="token number">22</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Student s6<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">"qianqi"</span><span class="token punctuation">,</span><span class="token number">24</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//將元素物件新增到集合物件中</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>s3<span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>s4<span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>s5<span class="token punctuation">)</span><span class="token punctuation">;</span> ts<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>s6<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//遍歷</span> <span class="token keyword">for</span><span class="token punctuation">(</span>Student s<span class="token operator">:</span>ts<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"-----------"</span><span class="token operator">+</span>s<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
}
- 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
Student.java:
public class Student {
private String name;
private int age;
<span class="token keyword">public</span> <span class="token function">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// TODO Auto-generated constructor stub</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token function">Student</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span> <span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span>String name<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> age<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 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
MyComparator類:
public class MyComparator implements Comparator<Student> {
<span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">compare</span><span class="token punctuation">(</span>Student s1<span class="token punctuation">,</span>Student s2<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 姓名長度</span> <span class="token keyword">int</span> num <span class="token operator">=</span> s1<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> s2<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 姓名內容</span> <span class="token keyword">int</span> num2 <span class="token operator">=</span> num <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">?</span> s1<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">compareTo</span><span class="token punctuation">(</span>s2<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">:</span> num<span class="token punctuation">;</span> <span class="token comment">// 年齡</span> <span class="token keyword">int</span> num3 <span class="token operator">=</span> num2 <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">?</span> s1<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> s2<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> num2<span class="token punctuation">;</span> <span class="token keyword">return</span> num3<span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
執行結果:
lis-----------22 qianqi-----------24 wangwu-----------24 chenliu-----------26 zhangsan-----------20 zhangsan-----------22
(三). 效能測試
物件類:
class Dog implements Comparable<Dog> {
int size;
public Dog(int s) {
size = s;
}
public String toString() {
return size + "";
}
@Override
public int compareTo(Dog o) {
//數值大小比較
return size - o.size;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
主類:
public class MyClass {
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> Random r <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> HashSet<span class="token generics function"><span class="token punctuation"><</span>Dog<span class="token punctuation">></span></span> hashSet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token generics function"><span class="token punctuation"><</span>Dog<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> TreeSet<span class="token generics function"><span class="token punctuation"><</span>Dog<span class="token punctuation">></span></span> treeSet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics function"><span class="token punctuation"><</span>Dog<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> LinkedHashSet<span class="token generics function"><span class="token punctuation"><</span>Dog<span class="token punctuation">></span></span> linkedSet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">LinkedHashSet</span><span class="token generics function"><span class="token punctuation"><</span>Dog<span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// start time</span> <span class="token keyword">long</span> startTime <span class="token operator">=</span> System<span class="token punctuation">.</span><span class="token function">nanoTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">1000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> x <span class="token operator">=</span> r<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token number">1000</span> <span class="token operator">-</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">;</span> hashSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Dog</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// end time</span> <span class="token keyword">long</span> endTime <span class="token operator">=</span> System<span class="token punctuation">.</span><span class="token function">nanoTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">long</span> duration <span class="token operator">=</span> endTime <span class="token operator">-</span> startTime<span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"HashSet: "</span> <span class="token operator">+</span> duration<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// start time</span> startTime <span class="token operator">=</span> System<span class="token punctuation">.</span><span class="token function">nanoTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">1000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> x <span class="token operator">=</span> r<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token number">1000</span> <span class="token operator">-</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">;</span> treeSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Dog</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// end time</span> endTime <span class="token operator">=</span> System<span class="token punctuation">.</span><span class="token function">nanoTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> duration <span class="token operator">=</span> endTime <span class="token operator">-</span> startTime<span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"TreeSet: "</span> <span class="token operator">+</span> duration<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// start time</span> startTime <span class="token operator">=</span> System<span class="token punctuation">.</span><span class="token function">nanoTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">1000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> x <span class="token operator">=</span> r<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token number">1000</span> <span class="token operator">-</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">;</span> linkedSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Dog</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// end time</span> endTime <span class="token operator">=</span> System<span class="token punctuation">.</span><span class="token function">nanoTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> duration <span class="token operator">=</span> endTime <span class="token operator">-</span> startTime<span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"LinkedHashSet: "</span> <span class="token operator">+</span> duration<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 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
執行結果:
HashSet: 1544313 TreeSet: 2066049 LinkedHashSet: 629826 雖然測試不夠準確,但能反映得出,TreeSet要慢得多,因為它是有序的。
相關推薦
JAVA學習篇14——集合詳解
概述: List , Set, Map都是介面,前兩個繼承至Collection介面,Map為獨立介面 Set下有HashSet,LinkedHashSet,TreeSet List下有ArrayList,Vector,LinkedList Map下有Hashta
JAVA學習篇14——map集合
Map: java體系中非常重要的資料結構之一 以鍵值對的形式進行儲存元素 底層實現方法:陣列+連結串列 Map介面 實現:HashMap HashTable TreeMap LinkedHashMap Map核心API:
J2EE學習篇之--Struts1詳解
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
JavaWeb學習篇之----Jsp詳解
今天我們來看一下Jsp的相關知識,首先來看看一下Jsp的相關定義: 簡介: JSP全稱是JavaServer Pages,它和servle技術一樣,都是SUN公司定義的一種用於開發動態web資源的技術。 JSP這門技術的最大的特點在於,寫jsp就像在寫html,但: 它相
Java中的Map集合詳解
一、概述 java中的map集合使用鍵(key)值(value)來儲存資料,其中值(value)可以重複,但鍵(key)必須是唯一,也可以為空,但最多隻能有一個key為空,它的主要實現類有HashM
Java中JNI的使用詳解第三篇:JNIEnv型別中方法的使用
上一篇說道JNIEnv中的方法的用法,這一篇我們就來通過例子來看一下這些方法的使用: 首先是第一個例子:在Java程式碼中定義一個屬性,然後再C++程式碼中將其設定成另外的值,並且輸出來 先來看一下Java程式碼: 在來看一下C++程式碼: #include&
Java中JNI的使用詳解第二篇:JNIEnv型別和jobject型別的解釋
上一篇說的是一個簡單的應用,說明JNI是怎麼工作的,這一篇主要來說一下,那個本地方法sayHello的引數的說明,以及其中方法的使用 首先來看一下C++中的sayHello方法的實現:  對於這個方法引數中的JNIEnv* env引數的解釋: JNIEnv型別實際上代表了Java
Java中JNI的使用詳解第一篇:HelloWorld
今天開始研究JNI技術,首先還是老套路,輸出一個HelloWorld:具體流程如下:在Java中定義一個方法,在C++中實現這個方法,在方法內部輸出“Hello World",然後再回到Java中進行呼叫。分為以下步驟: 第一步:在Eclipse中建立一個類:JNIDemo
Java中JNI的使用詳解第一篇 HelloWorld
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
【備戰春招/秋招系列】美團Java面經總結進階篇 (附詳解答案)
一 訊息佇列MQ的套路 1.1 介紹一下訊息佇列MQ的應用場景/使用訊息佇列的好處 ①.通過非同步處理提高系統性能 ②.降低系統耦合性 1.2 那麼使用訊息佇列會帶來什麼問題?考慮過這個問題嗎? 1.3 介紹
【備戰春招/秋招系列】美團Java面經總結終結篇 (附詳解答案)
該文已加入開源專案:JavaGuide(一份涵蓋大部分Java程式設計師所需要掌握的核心知識的文件類專案,Star 數接近 14 k)。地址:github.com/Snailclimb/…. 系列文章: 【備戰春招/秋招系列1】程式設計師的簡歷就該這樣寫 【備戰春招/秋招系列2】初出茅
【搞定Java併發程式設計】第9篇:CAS詳解
上一篇:volatile關鍵字詳解:https://blog.csdn.net/pcwl1206/article/details/84881395 目 錄: 一、CAS基本概念 1.1、CAS的定義 1.2、CAS的3個運算元 二、Java如何實現原子操作
【搞定Java併發程式設計】第6篇:ThreadLocal詳解
上一篇:synchronized關鍵字:https://blog.csdn.net/pcwl1206/article/details/84849400 目 錄: 1、ThreadLocal是什麼? 2、ThreadLocal使用示例 3、ThreadLocal原始碼分析
Java反射reflect學習 Class.forName()用法詳解
恰巧前段時間面試的時候問到了Java的反射,當時答得不是很好,正好抽空重新溫習下,本文只講解下通過反射獲取到類的屬性、構造方法、方法,關於類載入器(ClassLoader)將在其他文章中有具體介紹,本文不作介紹。 Class.forName()用法詳解 主要功能 Class.forN
Java中的集合詳解,結合 ArrayList、HashSet 的區別以及HashCode的作用。
Java中的集合: (1)Collection List(有序,可重複) ArrayList
java 集合詳解及如何應用
1、結構圖 2、集合對比說明 有序 允許元素重複 同步 描述
Java Set集合詳解及Set與List的區別
Java中的Set集合是繼承Collection的介面,是一個不包含重複元素的集合。 下圖是Set集合的原始碼。 Set和List都是以介面都形式來進行宣告。Set主要包含三種存放資料型別都變數,分別是HashSet,LinkedHashSet,TreeSet
Java集合詳解
一、陣列和集合的比較 陣列不是面向物件的,存在明顯的缺陷,集合彌補了陣列的缺點,比陣列更靈活更實用,而且不同的集合框架類可適用不同場合。如下: 1:陣列能存放基本資料型別和物件,而集合類存放的都是物件的引用,而非物件本身! 2:陣列容易固定無法動態改變,集合類容量動態改變。
Java集合詳解--什麼是集合
什麼是集合 集合類存放於java.util包中。 集合類存放的都是物件的引用,而非物件本身,出於表達上的便利,我們稱集合中的物件就是指集合中物件的引用(reference)。 集合型別主要有3種:set(集)、list(列表)和map(對映)。 通俗
【集合詳解】Java集合總結(下)-常用集合類對比
今天我們主要看一張圖。 圖是無意中在網上發現的,個人覺得非常好。(時間久了就忘了出處,請原創原諒~~) 我們還是從左到右分析。 ①所有的類都用到Iterator則說明所有的集合類都有遍歷集合的方法。 ②LinkedList除了繼承AbstractSe