一:Collection接口
集合框架的根
通用方法
boolean contains(Object a)
boolean equals(Object a)
Iterator iterator()
int size()
void clear()
boolean add(Object a)
二:Set接口
扩展Collection接口
不允许重复元素
对 add()、equals() 和 hashcode() 方法添加了限制
HashSet和TreeSet是Set的实现
三:SortedSet接口
扩展了Set接口
元素按升序排序
重要异常
ClassCastException
NullPointerException
四:List接口
具有顺序的集合
扩展了Collection接口
元素可以通过其整型下标访问
可以包含重复元素
方法分类
定位方法get()、set()、add()、remove()、addAll()
搜索方法indexOf() 和 lastIndexOf()
ListIterator方法 listIterator() 和 subList()
五:Map接口
将键映射至值的对象
每个键最多都只能映射至一个值
重要方法
基本操作put()、get()、remove()、containsKey()、containsValue()、size() 和 isEmpty()
批操作 putAll() 和 clear()
集合视图 keySet()、values() 和 entrySet()
LinkedHashMap排序不需要实现Comparator接口,查询出来什么数据,就按照什么顺序添加数据;
import java.util.*;
class HashTableExample {
public static void main(String args[]) {
Hashtable h = new Hashtable();
Enumeration e;
String str;
double bal;
h.put("Maria", new Double(4545.50));
h.put("Joseph", new Double(2000.00));
h.put("Johnson", new Double(5000.00));
e = h.keys();
while (e.hasMoreElements()) {
str = (String) e.nextElement();
System.out.println(str + ": " + h.get(str));
}
System.out.println();
bal = ( (Double) h.get("Maria")).doubleValue();
h.put("Maria", new Double(bal + 1000));
System.out.println("Maria's new balance: " + h.get("Maria"));
}
}