轉載請註明出處:Java集合中List,Set以及Map等集合體系詳解(史上最全)java
— List 有序,可重複數組
—Set 無序,惟一安全
針對Collection集合咱們到底使用誰呢?(掌握)數據結構
惟一嗎?dom
是:Setide
排序嗎?函數
是:TreeSet或LinkedHashSet
否:HashSet
若是你知道是Set,可是不知道是哪一個Set,就用HashSet。性能
否:List測試
要安全嗎?this
是:Vector
否:ArrayList或者LinkedList查詢多:ArrayList
增刪多:LinkedList
若是你知道是List,可是不知道是哪一個List,就用ArrayList。
若是你知道是Collection集合,可是不知道使用誰,就用ArrayList。
若是你知道用集合,就用ArrayList。
說完了Collection,來簡單說一下Map.
上圖:
Map接口有三個比較重要的實現類,分別是HashMap、TreeMap和HashTable。
這就意味着:
1. 介紹
- TreeSet, LinkedHashSet and HashSet 在java中都是實現Set的數據結構
2. 相同點
- Duplicates elements: 由於三者都實現Set interface,因此三者都不包含duplicate elements
- Thread safety: 三者都不是線程安全的,若是要使用線程安全能夠Collections.synchronizedSet()
3. 不一樣點
- Performance and Speed: HashSet插入數據最快,其次LinkHashSet,最慢的是TreeSet由於內部實現排序
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); } 12345678910111213141516171819202122
運行結果:
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能夠實現對元素按照某種規則進行排序,例以下面的例子
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); } } } 1234567891011121314151617181920212223242526
運行結果:
17
18
19
20
22
23
24
測試類:
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()); } } } 12345678910111213141516171819202122232425
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; } } 1234567891011121314151617181920212223242526272829303132
結果報錯:
緣由分析:
因爲不知道該安照那一中排序方式排序,因此會報錯。
解決方法:
1.天然排序
2.比較器排序
天然排序要進行一下操做:
1.Student類中實現 Comparable接口
2.重寫Comparable接口中的Compareto方法
compareTo(T o) 比較此對象與指定對象的順序。 1 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; } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
運行結果:
lis-----------22
qianqi-----------24
wangwu-----------24
chenliu-----------26
zhangsan-----------20
zhangsan-----------22
比較器排序步驟:
1.單首創建一個比較類,這裏以MyComparator爲例,而且要讓其繼承Comparator接口
2.重寫Comparator接口中的Compare方法
compare(T o1,T o2) 比較用來排序的兩個參數。 1
3.在主類中使用下面的 構造方法
TreeSet(Comparator<? superE> comparator) 構造一個新的空 TreeSet,它根據指定比較器進行排序。 12
測試類:
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()); } } } 123456789101112131415161718192021222324252627282930
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; } } 123456789101112131415161718192021222324252627282930313233
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; } } 1234567891011121314
運行結果:
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; } } 1234567891011121314
主類:
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); } } 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
運行結果:
HashSet: 1544313 TreeSet: 2066049 LinkedHashSet: 629826 雖然測試不夠準確,但能反映得出,TreeSet要慢得多,由於它是有序的。