java提供了大量的持有对象的方式:
1)数组将数字和对象联系起来,它保存类型明确的对象,查询对象时,不需要对结果做类型转换,它可以时多维的,可以保存基本数据类型的数据,但是,数组一旦生成,其容量就不能改变
2)Collection保存单一的元素,而Map保存相关联的键值对.有了java泛型,你就可以指定容器中存放的对象类型,因此你就不会将错误类型的对象放置到容器中,并且从容器中取出元素时,不必进行类型转换,各种Collection和各种Map都快以在你向其中添加更多的元素时,自动调整其尺寸.容器不能持有基本数据类型,但是自动包装机制会仔细地执行基本数据类型到容器中所持有的包装器类型之间的双向切换.
3)像数组一样,List也建立数字索引与对象的关联,因此,数组和List都是排好序的容器,List能够自动扩充容量
4)如果要进行大量的随机访问就用ArrayList,如果经常从表中插入或删除元素,则应该使用LinkedList
5)各种Queue以及栈的行为,由LinkedLIst提供支持
6)Map是一种将对象(而非数字)与对象相关联的设计,HashMap设计用来快速访问,而TreeMap保持"键"始终处于排序状态,所以没有HashMap快,LinkedHashMap保持元素插入的顺序,但是也通过散列提供了快速访问能力
&)Set不接受重复元素,HashSet提供最快的查询速度,而TreeSet保持元素处于排序状态,LinkedHashSet以插入顺序保存元素
8)新程序中不应该用过时的Vector,Hashtable和Stack
下面的Java容器的简图,包含了一般情况下会碰到的接口和类,可以看到,其实只有四种容器,Map, List, Set和Queue,它们各有两到三个实现版本(Queue的java.util.concurrent实现没有包含在上面这两个图中). 常用的容器用黑色粗线框表示, 点线框表示接口, 实线框表示普通的(具体的)类,带有空心箭头的点线表示一个特定的类实现了一个接口,实心箭头表示某个类可以生成箭头所指向类的对象,例如,任意的Colletion可以生成Iterator,而lIst可以生成LIstIterator(也可以生成普通的Iterator,因为List继承自Colletion)
下面的示例展示了各种不同的类在方法上的差异,可以看到除了TreeSet之外的所有Set都拥有与Collection完全一样的接口,List和Colletion存在着明显的不同,尽管所要求的方法都在Colletion中,另一方面,在Queue接口中的方法都是独立的,在创建具有Queue功能的实现时,不需要使用Colletion方法,最后Map和Colletion之间唯一的重叠就是Map可以使用entrySet()和values()方法来产生Colletion
注意,标记接口java.uitl.RandomAccess附着在ArrayList上,而没有附着在LinkedList上,这为想要根据所使用的特定的List而动态修改其行为的算法提供了信息
package object; //: holding/ContainerMethods.java import net.mindview.util.*; public class ContainerMethods { public static void main(String[] args) { ContainerMethodDifferences.main(args); } } /* Output: (Sample) Collection: [add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray] Interfaces in Collection: [Iterable] Set extends Collection, adds: [] Interfaces in Set: [Collection] HashSet extends Set, adds: [] Interfaces in HashSet: [Set, Cloneable, Serializable] LinkedHashSet extends HashSet, adds: [] Interfaces in LinkedHashSet: [Set, Cloneable, Serializable] TreeSet extends Set, adds: [pollLast, navigableHeadSet, descendingIterator, lower, headSet, ceiling, pollFirst, subSet, navigableTailSet, comparator, first, floor, last, navigableSubSet, higher, tailSet] Interfaces in TreeSet: [NavigableSet, Cloneable, Serializable] List extends Collection, adds: [listIterator, indexOf, get, subList, set, lastIndexOf] Interfaces in List: [Collection] ArrayList extends List, adds: [ensureCapacity, trimToSize] Interfaces in ArrayList: [List, RandomAccess, Cloneable, Serializable] LinkedList extends List, adds: [pollLast, offer, descendingIterator, addFirst, peekLast, removeFirst, peekFirst, removeLast, getLast, pollFirst, pop, poll, addLast, removeFirstOccurrence, getFirst, element, peek, offerLast, push, offerFirst, removeLastOccurrence] Interfaces in LinkedList: [List, Deque, Cloneable, Serializable] Queue extends Collection, adds: [offer, element, peek, poll] Interfaces in Queue: [Collection] PriorityQueue extends Queue, adds: [comparator] Interfaces in PriorityQueue: [Serializable] Map: [clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, values] HashMap extends Map, adds: [] Interfaces in HashMap: [Map, Cloneable, Serializable] LinkedHashMap extends HashMap, adds: [] Interfaces in LinkedHashMap: [Map] SortedMap extends Map, adds: [subMap, comparator, firstKey, lastKey, headMap, tailMap] Interfaces in SortedMap: [Map] TreeMap extends Map, adds: [descendingEntrySet, subMap, pollLastEntry, lastKey, floorEntry, lastEntry, lowerKey, navigableHeadMap, navigableTailMap, descendingKeySet, tailMap, ceilingEntry, higherKey, pollFirstEntry, comparator, firstKey, floorKey, higherEntry, firstEntry, navigableSubMap, headMap, lowerEntry, ceilingKey] Interfaces in TreeMap: [NavigableMap, Cloneable, Serializable] *///:~ //: net/mindview/util/ContainerMethodDifferences.java package object; import java.lang.reflect.*; import java.util.*; public class ContainerMethodDifferences { static Set<String> methodSet(Class<?> type) { Set<String> result = new TreeSet<String>(); for(Method m : type.getMethods()) result.add(m.getName()); return result; } static void interfaces(Class<?> type) { System.out.print("Interfaces in " + type.getSimpleName() + ": "); List<String> result = new ArrayList<String>(); for(Class<?> c : type.getInterfaces()) result.add(c.getSimpleName()); System.out.println(result); } static Set<String> object = methodSet(Object.class); static { object.add("clone"); } static void difference(Class<?> superset, Class<?> subset) { System.out.print(superset.getSimpleName() + " extends " + subset.getSimpleName() + ", adds: "); Set<String> comp = Sets.difference( methodSet(superset), methodSet(subset)); comp.removeAll(object); // Don't show 'Object' methods System.out.println(comp); interfaces(superset); } public static void main(String[] args) { System.out.println("Collection: " + methodSet(Collection.class)); interfaces(Collection.class); difference(Set.class, Collection.class); difference(HashSet.class, Set.class); difference(LinkedHashSet.class, HashSet.class); difference(TreeSet.class, Set.class); difference(List.class, Collection.class); difference(ArrayList.class, List.class); difference(LinkedList.class, List.class); difference(Queue.class, Collection.class); difference(PriorityQueue.class, Queue.class); System.out.println("Map: " + methodSet(Map.class)); difference(HashMap.class, Map.class); difference(LinkedHashMap.class, HashMap.class); difference(SortedMap.class, Map.class); difference(TreeMap.class, Map.class); } } ///:~ //: net/mindview/util/Sets.java package object; import java.util.*; public class Sets { public static <T> Set<T> union(Set<T> a, Set<T> b) { Set<T> result = new HashSet<T>(a); result.addAll(b); return result; } public static <T> Set<T> intersection(Set<T> a, Set<T> b) { Set<T> result = new HashSet<T>(a); result.retainAll(b); return result; } // Subtract subset from superset: public static <T> Set<T> difference(Set<T> superset, Set<T> subset) { Set<T> result = new HashSet<T>(superset); result.removeAll(subset); return result; } // Reflexive--everything not in the intersection: public static <T> Set<T> complement(Set<T> a, Set<T> b) { return difference(union(a, b), intersection(a, b)); } } ///:~