概述:
- List , Set, Map都是接口;List , Set繼承至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<>(); for (String data : Arrays.asList("B", "E", "D", "C", "A")) { hashSet.add(data); linkedHashSet.add(data); treeSet.add(data); } //不保證有序 System.out.println("Ordering in HashSet :" + hashSet); //FIFO保證安裝插入順序排序 System.out.println("Order of element in LinkedHashSet :" + linkedHashSet); //內部實現排序 System.out.println("Order of objects in TreeSet :" + treeSet);
}
運行結果:
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 { public static void main(String[] args) { // 創建集合對象 // 自然順序進行排序 TreeSet<Integer> ts = new TreeSet<Integer>(); // 創建元素並添加 // 20,18,23,22,17,24,19,18,24 ts.add(20); ts.add(18); ts.add(23); ts.add(22); ts.add(17); ts.add(24); ts.add(19); ts.add(18); ts.add(24); // 遍歷 for (Integer i : ts) { System.out.println(i); } } }
運行結果:
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); //將元素對象添加到集合對象中 ts.add(s1); ts.add(s2); ts.add(s3); ts.add(s4); ts.add(s5); ts.add(s6); //遍歷 for(Student s:ts){ System.out.println(s.getName()+"-----------"+s.getAge()); } } }
Student.java:
public class Student { private String name; private int age; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
結果報錯:

原因分析:
由於不知道該安照那一中排序方式排序,所以會報錯。
解決方法:
1.自然排序
2.比較器排序
(1).自然排序
自然排序要進行一下操作:
1.Student類中實現 Comparable接口
2.重寫Comparable接口中的Compareto方法
compareTo(To) 比較此對象與指定對象的順序。
public class Student implements Comparable<Student>{ private String name; private int age; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int compareTo(Student s) { //return -1; //-1表示放在紅黑樹的左邊,即逆序輸出 //return 1; //1表示放在紅黑樹的右邊,即順序輸出 //return o; //表示元素相同,僅存放第一個元素 //主要條件 姓名的長度,如果姓名長度小的就放在左子樹,否則放在右子樹 int num=this.name.length()-s.name.length(); //姓名的長度相同,不代表內容相同,如果按字典順序此 String 對象位於參數字符串之前,則比較結果為一個負整數。 //如果按字典順序此 String 對象位於參數字符串之后,則比較結果為一個正整數。 //如果這兩個字符串相等,則結果為 0 int num1=num==0?this.name.compareTo(s.name):num; //姓名的長度和內容相同,不代表年齡相同,所以還要判斷年齡 int num2=num1==0?this.age-s.age:num1; return num2; } }
運行結果:
lis-----------22
qianqi-----------24
wangwu-----------24
chenliu-----------26
zhangsan-----------20
zhangsan-----------22
(2).比較器排序
比較器排序步驟:
1.單獨創建一個比較類,這里以MyComparator為例,並且要讓其繼承Comparator接口
2.重寫Comparator接口中的Compare方法
compare(T o1,T o2) 比較用來排序的兩個參數。
3.在主類中使用下面的 構造方法
TreeSet(Comparator<? superE> comparator) 構造一個新的空 TreeSet,它根據指定比較器進行排序。
測試類:
public class MyClass { public static void main(String[] args) { //創建集合對象 //TreeSet(Comparator<? super E> comparator) 構造一個新的空 TreeSet,它根據指定比較器進行排序。 TreeSet<Student> ts=new TreeSet<Student>(new MyComparator()); //創建元素對象 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); //將元素對象添加到集合對象中 ts.add(s1); ts.add(s2); ts.add(s3); ts.add(s4); ts.add(s5); ts.add(s6); //遍歷 for(Student s:ts){ System.out.println(s.getName()+"-----------"+s.getAge()); } } }
Student.java:
public class Student { private String name; private int age; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
MyComparator類:
public class MyComparator implements Comparator<Student> { @Override public int compare(Student s1,Student s2) { // 姓名長度 int num = s1.getName().length() - s2.getName().length(); // 姓名內容 int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num; // 年齡 int num3 = num2 == 0 ? s1.getAge() - s2.getAge() : num2; return num3; } }
運行結果:
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; } }
主類:
public class MyClass { public static void main(String[] args) { Random r = new Random(); HashSet<Dog> hashSet = new HashSet<Dog>(); TreeSet<Dog> treeSet = new TreeSet<Dog>(); LinkedHashSet<Dog> linkedSet = new LinkedHashSet<Dog>(); // start time long startTime = System.nanoTime(); for (int i = 0; i < 1000; i++) { int x = r.nextInt(1000 - 10) + 10; hashSet.add(new Dog(x)); } // end time long endTime = System.nanoTime(); long duration = endTime - startTime; System.out.println("HashSet: " + duration); // start time startTime = System.nanoTime(); for (int i = 0; i < 1000; i++) { int x = r.nextInt(1000 - 10) + 10; treeSet.add(new Dog(x)); } // end time endTime = System.nanoTime(); duration = endTime - startTime; System.out.println("TreeSet: " + duration); // start time startTime = System.nanoTime(); for (int i = 0; i < 1000; i++) { int x = r.nextInt(1000 - 10) + 10; linkedSet.add(new Dog(x)); } // end time endTime = System.nanoTime(); duration = endTime - startTime; System.out.println("LinkedHashSet: " + duration); } }
運行結果:
HashSet: 1544313
TreeSet: 2066049
LinkedHashSet: 629826
雖然測試不夠准確,但能反映得出,TreeSet要慢得多,因為它是有序的。