AndyLee 2017.5.4
在集合中的元素沒有特定的順序,但是元素不能重複出現(是唯一的)
利用雜湊表(hash table)演算法改進執行的效率
實作的方式,是利用 hash code 產生並規劃出多個 hashcan,也可以根據 hash code 快速的找到特定的 object
若要判斷 HashSet 中的兩個 object 是否相同,除了使用 hashCode() method 來確定傳回值是否相同,還要使用 equals() method 來比較兩 object 是否相同。
HashSet物件裡的元素都是唯一存在的
宣告一個泛型型態為Integer的HashSet類別之物件hashset
Hashset<Integer> set = new Hashset<Integer>();
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
public class test
{
public static void main(String \[\] argv)
{
Set<String> set = new HashSet<String>();
set.add("張三");
set.add("王五");
set.add("李四");
set.add("張三");
for(int i : set){
System.out.print(i+", ");
}
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
}
}
Set<String> set = new TreeSet<String>();
增加
刪除
修改
查詢
由於使用 LinkedList 使用鏈結串列,在進行插入與移除動作時有較好的效能,適合拿來實作堆疊(Stack)與佇列(Queue)。
LinkedList實做Stack
public class StringStack {
private LinkedList<String> linkedList;
public StringStack() {
linkedList = new LinkedList<String>();
}
public void push(String name) {
linkedList.addFirst(name);
}
public String top() {
return linkedList.getFirst();
}
public String pop() {
return linkedList.removeFirst();
}
public boolean isEmpty() {
return linkedList.isEmpty();
}
LinkedList實作
public class StringQueue {
private LinkedList<String> linkedList;
public StringQueue() {
linkedList = new LinkedList<String>();
}
public void put(String name) {
linkedList.addFirst(name);
}
public String get() {
return linkedList.removeLast();
}
public boolean isEmpty() {
return linkedList.isEmpty();
}
}
List<String> list = new ArrayList<String>();
import java.util.ArrayList;
import java.util.Iterator;
public class test {
public static void main(String \[\] argv) {
ArrayList<String> list = new ArrayList<String>();
//新增
list.add("張三");
list.add("李四");
list.add("王五");
//list大小
System.out.println(list.size());
//取得陣列內容
System.out.println(list.get(0));
//查詢list內是否有該字串
System.out.println(list.contains("王五"));
//查詢元素位置
System.out.println(list.indexOf("王五"));
//刪除特定元素
list.remove("王五");
System.out.println(list.size());
//判斷list是否為空
System.out.println(list.isEmpty());
//建立Iterator,Iterator的是針對在不同的物件(陣列、鏈結、雜湊集合)的公開存取介面
//Iterator會先到物件內部收集資料再回傳到Iterator
Iterator it = list.iterator();
while(it.hasNext()){
System.out.print(it.next() + " ");
}
}
}
在 J2SE 5.0 之後新增了泛型(Generic)的功能,使用物件容器時可以宣告將儲存的物件型態,如此您的物件在存入容器會被限定為您所宣告的型態,編譯器在編譯時期會協助您進行型態檢查,而取出物件時也不至於失去原來的型態資訊,這可以避免型態轉換時的問題
Map<String, String> map = new HashMap<String, String>();
package net.twcic;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class HashMapDemo2 {
public static void main(String args[]) {
Map<String, String> map = new HashMap<String, String>();
map.put("leon", "leon 的資料");
map.put("godleon", "godleon 的資料");
map.put("bill", "bill 的資料");
// 透過 values() 取得 Collection
// 再由 Collection 變出 Iterator
// 最後由 Iterator 列出所有元素
Collection collection = map.values();
Iterator iterator = collection.iterator();
while(iterator.hasNext())
System.out.println(iterator.next());
System.out.println();
// 透過 for each 也是可以的
for(String key : map.KeySet())
System.out.println(key+" : "+ map.get(key));
}
}
}
Map<String,String> map = new TreeMap<String, String>();
package net.twcic;
import java.util.Map;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String args[]) {
Map<String, String> map = new TreeMap<String, String>();
map.put("godleon", "godleon 的資料");
map.put("leon", "leon 的資料");
map.put("bill", "bill 的資料");
for(String s : map.values())
System.out.println(s);
System.out.println();
}
}
1.http://mslab.csie.asia.edu.tw/~jackjow/courses/1001_WindowsProgram/ppt/16_Java collection.pdf
2.http://godleon.blogspot.tw/2007/07/container-container-list-set-map-key.html