java容器类总结
java容器类总结
1.java 容器分类图
说明:左图为简化图(其中粗线部分是重点的容器),右图为完整容器分类图


2. 容器类接口和抽象容器类
2.1 说明
容器接口是容器的基础。使用接口可以将容器的实现与容器接口分开,因而可以使用相同的方法访问容器而不需关心容器具体的数据结构。
同理,Iterator 接口也使用户能够使用相同的方法访问不同的容器类。
2.2 容器接口(Collection,Map,Iterator)
1)collection 接口
[
](javascript:void(0)😉
* boolean add(Object obj): 添加对象,集合发生变化则返回true
* Iterator iterator():返回Iterator接口的对象
* int size()
* boolean isEmpty()
* boolean contains(Object obj)
* void clear()
* <T> T[] toArray(T[] a)
[
](javascript:void(0)😉
2)Map 接口(存放键值对,Map 中的值也可以是一个容器)
[
](javascript:void(0)😉
* Object get(Object key)
* Object put(Object key, Object value)
* Set keySet() : returns the keys set Set<K> keySet()
* Set entrySet(): returns mappings set Set<Map.Entry<K,V>> entrySet()
* containsKey()
* containsValue()
[
](javascript:void(0)😉
3)Iterator 接口
* Object next()
* boolean hasNext()
* void remove()
注意:remove 函数不能连续执行多次,否则返回 IllegalStateException
( if the next method has not yet been called, or the remove method has already been called after the last call to the next method.)
通常用法:
[
](javascript:void(0)😉
Iterator it=collection.iterator();
while(it.hasNext())
{
Object obj=it.next();
//do something
}
[
](javascript:void(0)😉
2.3 子接口(List,Set,ListIterator,SortedMap,SortedSet)
1)List(有顺序可以重复,有顺序所以操作时可以在方法中加入索引参数,如下:)
* boolean add(E element)
* void add(int index, E element)
* E set(int index, E element)
* E get(int index);
2)Set(无顺序不可以重复,无序因而不能通过索引操作对象)
3)ListIterator(Iterator for List,List 是双向表,因而在 Iterator 上增加了一些新的方法,允许 traverse the List in either direction)
* boolean hasPrevious();
* E previous();
* int previousIndex()
4) SortedMap
说明:保证按照键的升序排列的映射,可以按照键的自然顺序( Comparable 接口)进行排序, 或者通过创建有序映射时提供的比较器进行排序
Map
Comparator
[
](javascript:void(0)😉
public interface SortedMap<K,V>extends Map<K,V>
* Comparator comparator()
* Object firstKey()
* Object lastKey()
[
](javascript:void(0)😉
5)SortedSet
主要用于排序操作,实现此接口的子类都是排序的子类
[
](javascript:void(0)😉
public interface SortedSet<E>extends Set<E>
* Comparator comparator()
* E first() :返回第一个元素
* E last()
* SortedSet<E> headSet(E toElement): 返回less than toElement
* SortedSet<E> tailSet(E fromElement)
* SortedSet<E> subSet(E fromElement)
[
](javascript:void(0)😉
2.4 抽象容器类
1)说明:使用抽象容器类可以方便的定义类,而不用在每个类中都实现容器接口 container 中的所有的方法
2)包含:
[
](javascript:void(0)😉
* AbstractCollection public abstract class AbstractCollection<E>extends Objectimplements Collection<E>
* AbstractList public abstract class AbstractList<E>extends AbstractCollection<E>implements List<E>
* AbstractSet public abstract class AbstractSet<E>extends AbstractCollection<E>implements Set<E>
* AbstactMap public abstract class AbstractMap<K,V>extends Object implements Map<K,V>
* AbstractSequentialList public abstract class AbstractSequentialList<E> extends AbstractList<E>
[
](javascript:void(0)😉
3. 具体容器类
3.1 概括
1)collection: ArrayList,LinkedLsit,Vector,Stack
TreeSet,HashSet,LinkedHashSet
- Map: HashMap,LinkedHashMap,WeakHashMap, TreeMap, HashTable, IdentityHashTable(其中 key 的比较是通过 == 而不是 equals)
3.2 常用的容器类
1)ArrayList 与 LinkedList(均非同步,多线程时需要考虑线程安全问题),Vector(同步),Stack
1. List 接口支持通过索引的方法来访问元素:ArrayList 随机访问快改慢;LinkedList 改快随机访问慢;Vector 实现了同步,因而比 ArrayList 慢
2. LinkedList 使用双向链表实现 LinkedList 提供额外的 get,remove,insert 方法在 LinkedList 的首部或尾部。这些操作使 LinkedList 可被用作堆栈(stack),队列(queue)或双向队列(deque)。
3. ArrayList 没有定义增长算法,当需要插入大量元素是,可调用 ensureCapacity 方法提高添加效率
4. Vector 类似与 ArrayList,但是是同步的,多线程安全(另外一点区别是 ArrayList 扩容时默认增长一半,Vector 增长一倍)。无论是单线程还是多线程,Vector 都比 ArrayList 慢
5. Stack 继承自 Vector,实现一个后进先出的堆栈
6. 若需要实现同步可以调用 Collections 工具类的 synchronizedList 方法,如下:
[
](javascript:void(0)😉
List list = Collections.synchronizedList(new ArrayList());
synchronized(list) {
Iterator i = list.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
或者:
List list = Collections.synchronizedList(new LinkedList());
[
](javascript:void(0)😉
7. 定义如下:(注意 LinkedList 实现了 Deque)
[
](javascript:void(0)😉
public class ArrayList<E>extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, Serializable
public class LinkedList<E>extends AbstractSequentialList<E>implements List<E>, Deque<E>, Cloneable, Serializable
public class Vector<E>extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, Serializable
[
](javascript:void(0)😉
2)TreeSet, HashSet, LinkedHashSet(HashSet,TreeSet 不是线程安全的)
1. TreeSet 是 SortedSet 接口的唯一实现类,TreeSet 可以确保集合元素处于排序状态,效率很高,可提高程序的效率;TreeSet 通过 compareTo 或者 compare 排序,因而只要值相等即使 equals 不等(不同对象)也不能加到集合中(fails to obey Set interface)
2. HashSet,效率很高,和 TreeSet 不同的是通过比较对象的 equals 区分不同对象,这样不同的对象可以不被重复的加入到集合中。
hashCode() 函数不好确定,对象默认的 hashCode 函数试对象的内存地址值,hashCode 函数的好坏是 HashSet 性能的关键。
3. LinkedHashSet, 和 HashSet 相同,同样是根据元素的 hashCode 值来决定元素的存储位置,但是它同时使用链表维护元素的次序。LinkedHashSet 在迭代访问 Set 中的全部元素时,性能比 HashSet 好,但是插入时性能稍微逊色于 HashSet。
4. Set 可以插入 null,最多一个 null
- HashMap(非同步), HashTable(线程安全), TreeMap, WeakHashMap
1.HashTable 与 HashMap 区别:(详情请查看 HashTable 与 HashMap)
1) Hashtable 继承自 Dictionary 类,而 HashMap 继承自 AbstractMap 类。但二者都实现了 Map 接口。
2) Hashtable 中的方法是 Synchronize 的,而 HashMap 中的方法在缺省情况下是非 Synchronize 的
3)Hashtable 中,key 和 value 都不允许出现 null 值;HashMap 中,null 可以作为键,这样的键只有一个;可以有一个或多个键所对应 的值为 null
4) HashTable 直接使用对象的 hashCode。而 HashMap 重新计算 hash 值。
2. WeakHashMap 是一种改进的 HashMap,它对 key 实行 “弱引用”,WeakHashMap 使用元素的引用而不是值作为 key,也就是说必须在引用相同(a==b)的情况下才能找到相关的值。另外,如果一个 key 不再被外部所引用,那么该 key 可以被 GC 回收。
\3. TreeMap 是 SortedMap 接口的基于红黑树的实现。此类保证了映射按照升序顺序排列关键字, 根据使用的构造方法不同,可能会按照键的类的自然顺序进行排序
\4. 定义如下:
public class Hashtable extends Dictionary implements Map, Cloneable, Serializable
public class HashMap extends AbstractMap implements Map, Cloneable, Serializable
public class TreeMap<K,V>extends AbstractMap<K,V>implements NavigableMap<K,V>, Cloneable, Serializable
4. 容器类使用补充
1)使用抽象编程思想,创建时使用父类引用指向子类对象,返回时返回抽象接口
2)如果涉及到堆栈,队列等操作,应该考虑用 List,对于需要快速插入,删除元素,应该使用 LinkedList,如果需要快速随机访问元素,应该使用 ArrayList。
3)如果程序在单线程环境中使用非同步的类,其效率较高
4)可以使用 Collections 工具类中 unmodifiableList/unmodifiableMap/unmodifiableSet/unmodifiableSortedMap/unmodifiableSortedSet 等创建不能修改的 List,Map,List 等
5)可以使用 Collections 工具类中 Collections.synchronizedList(new ArrayList()) 等实现同步
6) 可以使用 Arrays.equal() 判断两个数组是否相等

浙公网安备 33010602011771号