Java集合主要由兩個介面衍伸出來Collection and Map
Collection的子介面有 Set, Queue, List
而分別為
Collection => 無序集合
Set => 無序集合,元素不可重複
Queue => 佇列
List =>有序結合,元素可以重複
Map=>每筆資料都是Key-Value組成
--------------------------------------------------------------------------------------------
所以需存取元素的話
Set => 根據元素本身來做存取
List => 根據索引值存取
Map =>根據關鍵字存取
--------------------------------------------------------------------------------------------
最常使用到的集合類別
TreeSet, HashSet, ArrayDeque, LinkedList, ArrayList, HashMap, TreeMap
--------------------------------------------------------------------------------------------
Set
有些實作的類別HashSet、TreeSet、EnumSet都是執行緒不安全的,
所以需手動保證該集合的同步性,可以用Collections的synchronizeSortedSet
包裝該集合。
HashSet
1.順序是無序,添加時有可能順序會變化
2.非同步機制
3.集合可以是null
4.依照hashCode值,決定儲存位置
5
6.存取元素是用hashcode快速尋找
1.維護元素的插入順序(效能略低於HashSet)
1.SortedSet介面實作出來
2.依照實際元素的大小進行排序(除非沒自訂排序)
3.如果要自訂排序,可以參考test1、test2檔案
1.元素必須為enum的列舉值
2.依照enum中的排列順序依序排列
--------------------------------------------------------------------------------------------
List
1.可依靠索引值進行插入、刪除、查詢
2.可往前迭代(previous)和往後迭代(iterator)
ArrayList、 Vector
1.可以動態配置陣列(可用ensureCapacity來一次配置,提高性能)
2.ArrayList執行緒安全、Vector執行緒不安全3.推薦使用ArrayList,因Vector太古老
--------------------------------------------------------------------------------------------
Queue
1
1.是用元素大小排列,並不是存放順序(並不是絕對標準的佇列實作)
1.雙端佇列
2.實作ArrayDeque、LinkedList
1.相比Stack(較古老),推薦使用
3.可以參考test3檔案,看輸出狀況為[3,2,1,5,4]
--------------------------------------------------------------------------------------------
LinkedList
1.實作Deque、List介面
2.雙端佇列
3.List功能
--------------------------------------------------------------------------------------------
Map
1.可以利用keySet返回所有key組成的set
2.實作HashMap、LinkedHashMap、SortedMap(interface)、TreeMap
HashMap
1.Hashtable是一個古老的Map實作類別
2.Hashtable是一個執行緒安全的實作,反之HashMap是不安全(但速度較快)
3.Hashtable不允許key and value為null,反之HashMap則可以
4.與Set一樣,無法保證元素順序
5. key請勿隨意使用程式修改值,容易找不到對應的值
1.插入時可以保持添加(put)順序(鏈結串列)
Properties
1.Hashtable的子類別
2.key、value都是字串類型
3.可用InputStream、OutputStream存取*.ini檔
4.可已把key-value對以XML檔的形式存放或載入
5.可以參考test4檔案
1.SortedMap介面實作出來
2.保證所有key-value處於有序狀態
1.相比於HashMap(強參照),當key所參照的物件沒被其他強參照變數所參照,
則所參照的物件有可能被垃圾回收,而WeakHashMap也會自動刪除所對應
的Key-Value
1.處理新增進來的key-value比較特殊,當兩個key嚴格相等時(key1==key2,true)
才會認為兩個key相等(正常來說key1.equals(key2),true 和 hashCode相等就行了)
1.元素必須為enum的列舉值
2.依照enum中的排列順序依序排列
3.不允許key為null,但value可以
*陣列元素是連續的,且長度是固定,無法自由增加長度,但hashCode是
依照hashCode值運算其儲存位置,所以可以自由增加長度。
*一般來說,陣列(Array)是一塊連續的記憶體,所以存取時效能最好,而以鏈結
串列(Linked)來說,執行插入、刪除操作時有較好效能。
*Java先實作Map,然後通過包裝一個所有value都為null的Map集合,
實作出Set集合類別,所以Set和Map關係非常密切。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void test3(){ | |
ArrayDeque arrayDeque=new ArrayDeque(); | |
arrayDeque.add("5"); | |
arrayDeque.push("1"); | |
arrayDeque.push("2"); | |
arrayDeque.push("3"); | |
arrayDeque.add("4"); | |
System.out.println(arrayDeque); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void test1(){ | |
TreeSet treeSet=new TreeSet(new a()); | |
treeSet.add(3); | |
treeSet.add(1); | |
treeSet.add(2); | |
System.out.println(treeSet); | |
treeSet.comparator(); | |
} | |
class a implements Comparator{ | |
@Override | |
public int compare(Object o1, Object o2) { | |
// TODO Auto-generated method stub | |
Integer i1=(Integer)o1; | |
Integer i2=(Integer)o2; | |
return i2-i1; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void test2(){ | |
TreeSet treeSet=new TreeSet((o1,o2)->{ | |
Integer i1=(Integer)o1; | |
Integer i2=(Integer)o2; | |
return i1>i2?-1 | |
:i1<i2?1:0; | |
}); | |
treeSet.add(3); | |
treeSet.add(1); | |
treeSet.add(2); | |
System.out.println(treeSet); | |
treeSet.comparator(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void test4() throws Exception{ | |
Properties properties=new Properties(); | |
properties.setProperty("a", "1"); | |
properties.setProperty("b", "2"); | |
properties.store(new FileOutputStream("a.ini"), "test"); | |
Properties properties2=new Properties(); | |
properties2.setProperty("c", "3"); | |
properties2.load(new FileInputStream("a.ini")); | |
System.out.println(properties2); | |
} |
參考於細說Java 8 異常處理與圖形介面程式設計