Collection 是一個super interface in the collection framework
Map 是另外一個 super interface
List: is an ordered collection of items (允許重複實體的出現) 例如:ArrayList, LinkedList, Vector
Map: map keys to values, 一個key 最多隻能 map一個value。 key不能重複 但是value可以重複
Set: 包含了unique items, (不能重複)例如:HashSet, LinkedHashSet
*****************************************************************************************************************************************************************************************
【List】
ArrayList:
ArrayList實現了可變大小的數組。它允許所有元素,包括null
每個ArrayList實例都有一個容量(Capacity),即用於存儲元素的數組的大小。這個容量可隨着不斷添加新元素而自動增加
ArrayList是非同步的(unsynchronized)
import java.util.*;
public class arraylist
{
public static void main(String[] args)
{
ArrayList<String> myArr = new ArrayList<String>();
myArr.add("Italian Riviera");
myArr.add("Jersey Shore");
myArr.add("Puerto Rico");
myArr.add("Los Cabos Corridor");
myArr.add("Lubmin");
myArr.add("Coney Island");
myArr.add("Karlovy Vary");
myArr.add("Bourbon-l'Archambault");
myArr.add("Walt Disney World Resort");
myArr.add("Barbados");
String a = myArr.get(2);
System.out.println("The third Item is:" + a);
}
}
LinkedList:
鏈表解決了array和arraylist的一個重大缺陷, 那就是刪除問題。 在array和arraylist中刪除一個元素要付出很大的代價,所有的元素都要前移。
而在鏈表中刪除一個元素,則只需在被刪除元素附近的Node進行更新即可
import java.util.*;
public class linkedlist{
public static void main(String[] args)
{
List<String> a = new LinkedList<String>();
a.add("Amy");
a.add("Carl");
a.add("Erica1");
a.add("Erica2");
a.add("Erica3");
a.add("Erica4");
Iterator iter = a.iterator();
while(iter.hasNext())
{
System.out.println("The next item is: " + (String)iter.next());
}
}
}
Vector:
Vector非常類似ArrayList,但是Vector是同步的(synchronized)(Thread safe)。所以vector一般用於多線程
import java.util.*;
public class vector{
public static void main(String[] args)
{
Vector<String> myVector=new Vector<String>(10,2); //declare vector
//This program declares a Vector of size 10 and its space will increase by 2 when more then 10 elements are added.
String sample="tester"; //test string declared
myVector.add(sample); //adds sample's value to the vector
System.out.println("Value is :"+myVector.get(0));
}
}
*****************************************************************************************************************************************************************************************
Iterator<E> iterator() ------ 返回一個用於訪問集合內每個元素的迭代器
*****************************************************************************************************************************************************************************************
【Set】Set不允許重複元素,因此加入Set的Object必須定義equals()方法以確保對象的唯一性。
HashSet:
HashSet採用散列函數對元素進行排序,是專門爲快速查詢而設計的。存入HashSet的對象必須定義hashCode()。
Example:
TreeSet:
TreeSet採用紅黑樹的數據結構進行排序元素,能保證元素的次序,使用它可以從Set中提取有序的序列。
import java.util.*;
public class TestTreeSetCommon
{
public static void main(String[] args)
{
TreeSet nums = new TreeSet();
//向TreeSet中添加四個Interger對象
nums.add(5);
nums.add(2);
nums.add(10);
nums.add(-9);
//輸出集合元素,看到集合元素已經處於排序狀態
System.out.println(nums);
//輸出集合裏的第一個元素
System.out.println(nums.first());
//輸出集合裏的最後一個元素
System.out.println(nums.last());
//返回小於4的子集, 不包含4
System.out.println(nums.headSet(4));
//返回大於5的子集,如果Set中包含5,子集中還包含5
System.out.println(nums.tailSet(5));
//返回大於等於-3,小於4的子集
System.out.println(nums.subSet(-3,4));
}
}
LinkedHashSet:
LinkedHashSet內部使用散列以加快查詢速度,同時使用鏈表維護元素的插入的次序,在使用迭代器遍歷Set時,結果會按元素插入的次序顯示。
http://blog.csdn.net/lee576/article/details/7265870
http://www.blogjava.net/kissyan4916/articles/279119.html
*****************************************************************************************************************************************************************************************
【map】
Hashtable:
import java.util.*;
class HTDemo {
public static void main(String args[]) {
Hashtable balance = new Hashtable();
Enumeration names;
String str;
double bal;
balance.put("John Doe", new Double(3434.34));
balance.put("Tom Smith", new Double(123.22));
balance.put("Jane Baker", new Double(1378.00));
balance.put("Todd Hall", new Double(99.22));
balance.put("Ralph Smith", new Double(-19.08));
// Show all balances in hash table.
names = balance.keys();
while(names.hasMoreElements()) {
str = (String) names.nextElement();
System.out.println(str + ": " +
balance.get(str));
}
System.out.println();
// Deposit 1,000 into John Doe's account
bal = ((Double)balance.get("John Doe")).doubleValue();
balance.put("John Doe", new Double(bal+1000));
System.out.println("John Doe's new balance: " +
balance.get("John Doe"));
}
}
The output from this program is shown here:
Ralph Smith: -19.08
Tom Smith: 123.22
John Doe: 3434.34
Todd Hall: 99.22
Jane Baker: 1378.0
John Doe's new balance: 4434.34
HashMap:
import java.util.*;
class HashMapDemo {
public static void main(String args[]) {
// Create a hash map
HashMap hm = new HashMap();
// Put elements to the map
hm.put("John Doe", new Double(3434.34));
hm.put("Tom Smith", new Double(123.22));
hm.put("Jane Baker", new Double(1378.00));
hm.put("Todd Hall", new Double(99.22));
hm.put("Ralph Smith", new Double(-19.08));
// Get a set of the entries
Set set = hm.entrySet();
// Get an iterator
Iterator i = set.iterator();
// Display elements
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
// Deposit 1000 into John Doe's account
double balance = ((Double)hm.get("John Doe")).doubleValue();
hm.put("John Doe", new Double(balance + 1000));
System.out.println("John Doe's new balance: " +
hm.get("John Doe"));
}
}
output:
Ralph Smith: -19.08
Tom Smith: 123.22
John Doe: 3434.34
Todd Hall: 99.22
Jane Baker: 1378.0
John Doe's current balance: 4434.34
LinkedHashMap:
package devmanuals.com;
import java.util.*;
public class LinkedHashMapDemo {
public static void main(String args[]) {
LinkedHashMap Lhm = new LinkedHashMap();
Lhm.put(1, "Gyan");
Lhm.put(6, "Ankit");
Lhm.put(5, "Arun");
Lhm.put(4, "Anand");
Lhm.put(3, "Ram");
System.out.println("The Entries of LinkedHashMap are : " + Lhm);
}
}
Output:-
The Entries of LinkedHashMap are :
{1=Gyan, 6=Ankit, 5=Arun, 4=Anand, 3=Ram} |
TreeMap:
import java.util.*;
class TreeMapDemo {
public static void main(String args[]) {
// Create a tree map
TreeMap tm = new TreeMap();
// Put elements to the map
tm.put("John Doe", new Double(3434.34));
tm.put("Tom Smith", new Double(123.22));
tm.put("Jane Baker", new Double(1378.00));
tm.put("Todd Hall", new Double(99.22));
tm.put("Ralph Smith", new Double(-19.08));
// Get a set of the entries
Set set = tm.entrySet();
// Get an iterator
Iterator i = set.iterator();
// Display elements
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
// Deposit 1000 into John Doe's account
double balance = ((Double)tm.get("John Doe")).doubleValue();
tm.put("John Doe", new Double(balance + 1000));
System.out.println("John Doe's new balance: " +
tm.get("John Doe"));
}
}
The following is the output from this program:
Jane Baker: 1378.0
John Doe: 3434.34
Ralph Smith: -19.08
Todd Hall: 99.22
Tom Smith: 123.22
John Doe's current balance: 4434.34
*****************************************************************************************************************************************************************************************