1 完整的容器分类
2 实用方法
2.1 静态方法
max(Collection c);//自然比较法找出最大值
min(Collection c);//自然比较法找出最小值
max(Collection c, Comparator com);//自定义比较法找出最大值
min(Collection c, Comparator com);//自定义比较法找出最小值
indexOfSubList(List source, List target);//返回taget在source中第一次出现的位置,如果没有则返回-1
lastIndexOfSubList(List source, List target);//返回taget在source中第一次出现的位置,如果没有则返回-1
replaceAll(List<T> , T oldVal, T newVal);//使用newVal替代List里面所有的oldVal
reverse(List);//逆序List
reverseOrder();
reverseOrder(Comparator com);//按照自定义比较法,逆序
rotate(List, int distance);//所有元素向后移动distance位数,末尾的元素放在List开头
shuffle(List);//随机改变顺序
shuffle(List, Random);//随机改变顺序,按照自定义的随机机制
sort(List<T> );//自然比较法排序
sort(List<T>, Comparator);//自定义比较法排序
copy(List<? super T> dest, List<? extends T> src);//将src中的元素复制到dest中
swap(List, int i, int j);//交换List中,i和j位置元素
fill(List<? super T>, T x);//用对象x替换list中的元素
nCopies(int n, T x);//返回n个元素的List,元素引用都指向x,此List不可改变。
disjoint(Collection, Collection);//两个集合没有相同元素时,返回true
frequency(Collection, Object x);//返回集合中元素x的数量
emptyList();//返回不可变的空List,Map,Set
emptyMap();//
emptySet();//
singleton(T x);//产生不可变的Set,List,Map,都只包含给定的参数一项元素。
singletonList(T x);
singletonMap(K key, V value);
2.2 Collection和Map设置为不可修改
创建一个只读Collection或Map。
Collection<ClassName> c = Collection.unmodifiableCollection(new ArrayList<ClassName>());
Collection<ClassName> c = Collection.unmodifiableList(new ArrayList<ClassName>());
Collection<ClassName> c = Collection.unmodifiableSet(new HashSet<ClassName>());
Collection<ClassName> c = Collection.unmodifiableSortedSet(new TreeSet<ClassName>());
Collection<ClassName> c = Collection.unmodifiableMap(new HashMap<ClassName>());
Collection<ClassName> c = Collection.unmodifiableSortedMap(new TreeMap<ClassName>());
任何企图修改容器内容的操作都会引起UnsupportedOperationException异常。
2.3 Collection和Map的同步控制
创建一个Collection或Map,在多线程中,能够完成同步控制。
Collection<ClassName> c = Collection.synchronizedCollection(new ArrayList<ClassName>());
Collection<ClassName> c = Collection.synchronizedList(new ArrayList<ClassName>());
Collection<ClassName> c = Collection.synchronizedSet(new HashSet<ClassName>());
Collection<ClassName> c = Collection.synchronizedSortedSet(new TreeSet<ClassName>());
Collection<ClassName> c = Collection.synchronizedMap(new HashMap<ClassName>());
Collection<ClassName> c = Collection.synchronizedSortedMap(new TreeMap<ClassName>());
3 持有引用
java.lang.ref类库中包含一组类,为垃圾回收提供了很大的灵活性。 当存在可能耗尽内存的对象时,较为有用。
3.1 Reference分类
3.1.1 SoftReference
用以实现内存敏感的高速缓存。可选是否放入ReferenceQueue中,作为回收前的清理工具。
3.1.2 WeakReference
为实现规范映射,不妨碍垃圾回收器回收键或值,对象的实例可以在程序多处被使用,以节省内存空间。可选是否放入ReferenceQueue中,作为回收前的清理工具。
3.1.3 PhantomReference
用以调度回收前的清理工作,比Java终止机制更加灵活。只能依赖ReferenceQueue。
3.2 WeakHashMap
用来保存WeakReference,这是一种特殊的Map,在这种映射中,每一个值只保存一份实例,以节省空间。当程序需要那个值的时候,在现有对象中查找,然后使用,无需重新创建。
WeakHashMap允许垃圾回收器自动清理键和值。添加新的键值时,会自动自动使用WeakReference包装他。
4 Java 1.0/1.1的容器
这些是较老的容器,不建议使用,但是有些代码较老,可能存在这些容器。
- Vector和Enumeration
Vector可以看作是ArrayList,方法名较为难记,缺陷多。
Enumeration枚举。
- Hashtable
类似于HashMap的一种容器
- Stack
java.util.stack里面的Stack是从Vector继承的,设计上较差,若要使用栈,建议使用net.mindview.util.stack中的Stack类,这是用LinkedList类实现的。
- BitSet
存储大量开关的效率较高,但是访问效率低。而且最小容量是long,64位,如果存储内容较小,比如8位,那么就会浪费空间。