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
*****************************************************************************************************************************************************************************************