1、Java容器类库的简化图:下面是集合类库更加完备的图。包括抽象类和遗留构件(不包括Queue的实现):
图1 集合框架类系图
2、ArrayList初始化时不可指定容量,如果以new ArrayList()方式创建时,初始容量为10个;如果以new ArrayList(Collection c)初始化时,容量为c.size()*1.1,即增加10%的容量;当向ArrayList中添加一个元素时,先进行容器的容量调整,如果容量不够时,则增加至原来的1.5倍加1,再然后把元素加入到容器中,即以原始容量的0.5倍比率增加。
3、Vector:初始化时容量可以设定,如果以new Vector()方式创建时,则初始容量为10,超过容量时以2倍容量增加。如果以new Vector(Collection c)方式创建时,初始容量为c.size()*1.1,超过时以2倍容量增加。如果以new Vector(int initialCapacity, int capacityIncrement),则以capacityIncrement容量增加。
4、集合特点:
5、Hashtable和HashMap的区别:
int hash = key.hashCode();//直接使用键的hashCode方法求哈希值 //哈希地址转hash数组索引,先使用最大正int数与,这样将负转正数,再与数组长度求模得到存入的hash数组索引位置 int index = (hash & 0x7FFFFFFF) % tab.length;而HashMap重新计算hash值,而且用位运算&代替求模:
int hash = hash(k); int i = indexFor(hash, table.length); static int hash(Object x) { //以键本身的hash码为基础求哈希地址,但看不懂是什么意思 int h = x.hashCode(); h += ~(h << 9); h ^= (h >>> 14); h += (h << 4); h ^= (h >>> 10); return h; } static int indexFor(int h, int length) { return h & (length-1);//将哈希地址转换成哈希数组中存入的索引号 }HashMap实现图:
6、集合中键值是否允许null小结:
7、对List的选择:
8、对Set的选择:
9、对Map的选择:
10、Stack基于线程安全,Stack类是用Vector来实现的(public class Stack extends Vector),但最好不要用集合API里的这个实现栈,因为它继承于Vector,本就是一个错误的设计,应该是一个组合的设计关系。
11、Iterator对ArrayList(LinkedList)的操作限制:
if (e.hash == hash && eq(k, e.key))//先比对hashcode,再使用equals return true; static boolean eq(Object x, Object y) { return x == y || x.equals(y); }
String对象是可以准确做为键的,因为已重写了这两个方法。
因此,Java中的集合框架中的哈希是以一个对象查找另外一个对象,所以重写hasCode与equals方法很重要。
13、重写hashCode()与equals()这两个方法是针对哈希类,至于其它集合,如果要用public boolean contains(Object o)或containsValue(Object value)查找时,只需要实现equals()方法即可,他们都只使用对象的equals方法进行比对,没有使用hashCode方法。
14、TreeMap/TreeSet的元素比较:放入其中的元素一定要具有自然比较能力(即要实现java.lang.Comparable接口)或者在构造TreeMap/TreeSet时传入一个比较器(实现java.util.Comparator接口),如果在创建时没有传入比较器,而放入的元素也没有自然比较能力时,会出现类型转换错误(因为在没有较器时,会试着转成Comparable型)。
//自然比较器 public interface java.lang.Comparable { public int compareTo(Object o); } public interface java.util.Comparator { int compare(Object o1, Object o2); boolean equals(Object obj); }15、Collection或Map的同步控制:可以使用Collections类的相应静态方法来包装相应的集合类,使他们具线程安全,如publicstatic CollectionsynchronizedCollection (Collection c)方法实质返回的是包装后的SynchronizedCollection子类,当然你也可以使用Collections的synchronizedList、synchronizedMap、synchronizedSet方法来获取不同的经过包装了的同步集合,其代码片断:
public class Collections { //... static Collection synchronizedCollection(Collection c, Object mutex) { return new SynchronizedCollection(c, mutex); } public static List synchronizedList(List list) { //... } static Set synchronizedSet(Set s, Object mutex) { //... } public static Map synchronizedMap(Map m) { return new SynchronizedMap(m); } //... static class SynchronizedCollection implements Collection, Serializable { Collection c; // 对哪个集合进行同步(包装) Object mutex; // 对象锁,可以自己设置 //... SynchronizedCollection(Collection c, Object mutex) { this.c = c; this.mutex = mutex; } public int size() { synchronized (mutex) { return c.size(); } } public boolean isEmpty() { synchronized (mutex) { return c.isEmpty(); } } //... } static class SynchronizedList extends SynchronizedCollection implements List { List list; SynchronizedList(List list, Object mutex) { super(list, mutex); this.list = list; } public Object get(int index) { synchronized (mutex) { return list.get(index); } } //... } static class SynchronizedSet extends SynchronizedCollection implements Set { SynchronizedSet(Set s) { super(s); } //... } //... }
由包装的代码可以看出只是把原集合的相应方法放在同步块里调用罢了。
16、通过迭代器修改集合结构
在使用迭代器遍历集合时,我们不能通过集合本身来修改集合的结构(添加、删除),只能通过迭代器来操作,下面是拿对HashMap删除操作的测试,其它集合也是这样:
public static void main(String[] args) { Map map = new HashMap(); map.put(1, 1); map.put(2, 3); Set entrySet = map.entrySet(); Iterator it = entrySet.iterator(); while (it.hasNext()) { Entry entry = (Entry) it.next(); /* * 可以通过迭代器来修改集合结构,但前提是要在已执行过 next 或 * 前移操作,否则会抛异常:IllegalStateException */ // it.remove(); /* * 抛异常:ConcurrentModificationException 因为通过 迭代 器操 * 作时,不能使用集合本身来修 * 改集合的结构 */ // map.remove(entry.getKey()); } System.out.println(map); }
从上图可知,它们都实现了List接口。它们的用法差不多,主要的区别在于它们对于不同操作的操作速度不同。
ArrayList是可以改变大小的数组。当有元素添加到ArrayList中去时,它的大小动态的增加。元素可以直接通过get()和set()方法进行访问,因为ArrayList实际上是数组。 LinkedList是个双向链表。它的add()和remove()方法比ArrayList快,但是get()和set()方法却比ArrayList慢。Vector和ArrayList类似,但是Vector是同步的。如果在线程安全的环境下,使用ArrayList是更好的选择。添加元素的时候,当超过初始容量的时候,Vector和ArrayList需要更多的空间:Vector需要将数组的大小增加一倍,而ArrayList需要增加50%。
LinkedList还实现了Queue接口,这样就比ArrayList和Vector多出了一些方法如offer(), peek(), poll()等。
注意:ArrayList的初始容量(initial capacity)很小。当内存足够时,我们应该设置一个比较大的初始容量,这样可以避免重新改变大小。
Vector几乎和ArrayList相等,主要的区别在于Vector是同步的。正因为此,Vector比ArrayList的开销更大。通常大部分程序员都使用ArrayList,他们可以自己写代码进行同步。图3 ArrayList vs. LinkedList性能比较
* 表中的add()指的是add(E e)(即是在列表末尾添加元素),remove()方法指的是remove(int index)。
ArrayList对于任意索引的插入/删除操作的时间复杂度是O(n),而在列表的尾部的操作时间为O(1)。
LinkedList对于任意索引的插入/删除操作的时间复杂度是O(n),而在列表的头部或尾部的操作时间为O(1)。
使用下面的代码测试它们的性能:ArrayList<Integer> arrayList = new ArrayList<Integer>(); LinkedList<Integer> linkedList = new LinkedList<Integer>(); // ArrayList add long startTime = System.nanoTime(); for (int i = 0; i < 100000; i++) { arrayList.add(i); } long endTime = System.nanoTime(); long duration = endTime - startTime; System.out.println("ArrayList add: " + duration); // LinkedList add startTime = System.nanoTime(); for (int i = 0; i < 100000; i++) { linkedList.add(i); } endTime = System.nanoTime(); duration = endTime - startTime; System.out.println("LinkedList add: " + duration); // ArrayList get startTime = System.nanoTime(); for (int i = 0; i < 10000; i++) { arrayList.get(i); } endTime = System.nanoTime(); duration = endTime - startTime; System.out.println("ArrayList get: " + duration); // LinkedList get startTime = System.nanoTime(); for (int i = 0; i < 10000; i++) { linkedList.get(i); } endTime = System.nanoTime(); duration = endTime - startTime; System.out.println("LinkedList get: " + duration); // ArrayList remove startTime = System.nanoTime(); for (int i = 9999; i >=0; i--) { arrayList.remove(i); } endTime = System.nanoTime(); duration = endTime - startTime; System.out.println("ArrayList remove: " + duration); // LinkedList remove startTime = System.nanoTime(); for (int i = 9999; i >=0; i--) { linkedList.remove(i); } endTime = System.nanoTime(); duration = endTime - startTime; System.out.println("LinkedList remove: " + duration);输出如下:
ArrayList add: 13265642 LinkedList add: 9550057 ArrayList get: 1543352 LinkedList get: 85085551 ArrayList remove: 199961301 LinkedList remove: 85768810
图4 各List数据结构性能比较
它们的性能的差别很显著。LinkedList对于add()和remove()相对于ArrayList要快,但是get()要慢些。按照复杂度以及测试结果来看,我们很容易知道什么时候该使用ArrayList,什么时候该使用LinkedList。简而言之,下面的情况该使用LinkedList:
HashSet是采用hash表来实现的。其中的元素没有按顺序排列,add()、remove()以及contains()等方法都是复杂度为O(1)的方法。
TreeSet是采用树结构实现(红黑树算法)。元素是按顺序进行排列,但是add()、remove()以及contains()等方法都是复杂度为O(log (n))的方法。它还提供了一些方法来处理排序的set,如first(), last(), headSet(), tailSet()等等。
LinkedHashSet介于HashSet和TreeSet之间。它也是一个hash表,但是同时维护了一个双链表来记录插入的顺序。基本方法的复杂度为O(1)。
总体而言,如果你需要一个访问快速的Set,你应该使用HashSet;当你需要一个排序的Set,你应该使用TreeSet;当你需要记录下插入时的顺序时,你应该使用LinedHashSet。下面的代码测试了以上三个类的add()方法的性能。
public static void main(String[] args) { Random r = new Random(); HashSet<Dog> hashSet = new HashSet<Dog>(); TreeSet<Dog> treeSet = new TreeSet<Dog>(); LinkedHashSet<Dog> linkedSet = new LinkedHashSet<Dog>(); // start time long startTime = System.nanoTime(); for (int i = 0; i < 1000; i++) { int x = r.nextInt(1000 - 10) + 10; hashSet.add(new Dog(x)); } // end time long endTime = System.nanoTime(); long duration = endTime - startTime; System.out.println("HashSet: " + duration); // start time startTime = System.nanoTime(); for (int i = 0; i < 1000; i++) { int x = r.nextInt(1000 - 10) + 10; treeSet.add(new Dog(x)); } // end time endTime = System.nanoTime(); duration = endTime - startTime; System.out.println("TreeSet: " + duration); // start time startTime = System.nanoTime(); for (int i = 0; i < 1000; i++) { int x = r.nextInt(1000 - 10) + 10; linkedSet.add(new Dog(x)); } // end time endTime = System.nanoTime(); duration = endTime - startTime; System.out.println("LinkedHashSet: " + duration);从输出看来,HashSet是最快的:
HashSet: 2244768 TreeSet: 3549314 LinkedHashSet: 2263320注意这个测试并不是非常精确,但足以反映基本的情况。
Java SE中有四种常见的Map实现——HashMap, TreeMap, Hashtable和LinkedHashMap。如果我们使用一句话来分别概括它们的特点,就是:
Map的结构能够快速找到一个对象,而不是进行较慢的线性查找。使用hash过的键来定位对象分两步。Map可以看作是数组的数组,第一个数组的索引就是对键采用hashCode()计算出来的值,再在这个位置上查找第二个数组,使用键的equals()方法来进行线性查找,直到找到要找的对象。Object类中的hashCode()对于不同的对象返回不同的整数,不同的对象(即使相同的类型)也返回不同的hash值。可见HashMap不允许有两个相等的Key键存在。默认情况下(也就是Key的类型没有实现hashCode()和equals()方法时),会使用Object类中的这两个方法。equals()方法默认只比较两个引用是否指向同样的对象。因此对自定义的Key类型,你要重写hashCode()和equals(),使它们能做正确的比较。
Hash码就像是一个存储空间的序列,不同的东西放在不同的存储空间中。将不同的东西整理放在不同的空间中(而不是堆积在一个空间中)更高效。所以能够均匀的分散hash码是再好不过了。
TreeMap按照键的顺序进行排列对象,所以键的对象之间需要能够比较,所以就要实现Comparable接口。你可以使用已经实现了Comparable接口的类来作为键,如String。
对Hashtable,Java文档写道:HashMap类和Hashtable类几乎相同,不同之处在于HashMap是不同步的,也允许接受null键和null值。
LinkedHashMap是HashMap的子类,所以LinkedHashMap继承了HashMap的一些属性,它在HashMap基础上增加的特性就是保存了插入对象的顺序。
参考文献:
http://jiangzhengjun.iteye.com/blog/553191
http://www.programcreek.com/java-collections/