JAVA中的容器主要分为三类:Map类型、List类型和Set类型。通过实现接口方法的方式来集体实现某一个集合。而List和Set集合都继承了Collection接口,实现了它的方法,因此List类型的集合和Set类型的集合用法大同小异,区别就在于Set类型集合可以重复存储且无序,List类型集合是有序且不可重复存储的集合。Map类型集合采用键值对的存储方式(Key--Value),具体用法如下。
Map接口(“键、值”映射存储)——hashCode方法查找“键”
已知实现类
AbstractMap, Attributes, AuthProvider, ConcurrentHashMap, ConcurrentSkipListMap, EnumMap, HashMap, Hashtable, IdentityHashMap, LinkedHashMap, PrinterStateReasons, Properties, Provider, RenderingHints, SimpleBindings, TabularDataSupport, TreeMap, UIDefaults, WeakHashMap
常用:HashMap类,LinkedHashMap类,TreeMap类不是同步方法,HashTable类是同步方法。
常用方法:
int size()//返回Map集合大小
boolean isEmpty()//判断Map集合是否为空(true为空)
boolean containsKey(Object key)//判断Map结合中是否含有Key(true包含)
boolean containsValue(Object value)//判读Map集合中是否含有Value(true包含)
V get(Object key)//返回Key得到对应的Value
V put(K key,V value)//把对应的Key—Value保存到Map集合中
void putAll(Map<? extends K,? extends V> m)//把另一个Map集合存到这个Map集合中
V remove(Object key)//把某一个对应的Key—Value移出Map集合,返回Value
void clear()//清空Map集合中的所有键值对对
Collection接口常用方法:
boolean add(E e)
确保此 collection 包含指定的元素。
boolean addAll(Collection<? extends E> c)
将指定 collection 中的所有元素都添加到此 collection 中。
void clear()
移除此 collection 中的所有元素。
boolean contains(Object o)
如果此 collection 包含指定的元素,则返回 true。
boolean containsAll(Collection<?> c)
如果此 collection 包含指定 collection 中的所有元素,则返回 true。
boolean isEmpty()
如果此 collection 不包含元素,则返回 true。
Iterator<E> iterator()
返回在此 collection 的元素上进行迭代的迭代器。(遍历)
boolean remove(Object o)
从此 collection 中移除指定元素的单个实例,如果存在的话。
boolean removeAll(Collection<?> c)
移除此 collection 中那些也包含在指定 collection 中的所有元素。
boolean retainAll(Collection<?> c)
仅保留此 collection 中那些也包含在指定 collection 的元素。
int size()
返回此 collection 中的元素数。
Object[] toArray()
返回包含此 collection 中所有元素的数组。
<T> T[] toArray(T[] a)
返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。
Set接口实现了Collection接口(无序可重复存储)——equals方法
已知实现子类
AbstractSet, ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, JobStateReasons, LinkedHashSet, TreeSet
常用:LinkedHashSet, TreeSet,HashSet类实现了Set接口,ConcurrentSkipListSet, CopyOnWriteArraySet是线程安全的,其他都不是线程安全的,需要自己处理。
List接口实现了Collection接口(有序不可重复存储)——equals方法
比较大小需要实现comparable接口,重写compareTo()方法,用来比较大小排序。
已知实现子类
AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector
常用:ArrayList类,LinkedList类不是同步方法,CopyOnWriteArrayList是同步方法。
容器类对象在调用remove,contains等方法时需要比较对象是否相等,涉及到equals方法和hashCode方法;对于用户自定义的类型,需要重写这两个方法。
Iterator接口(集合类都实现了该方法)
Map接口(“键、值”映射存储)——hashCode方法查找“键”
已知实现类
AbstractMap, Attributes, AuthProvider, ConcurrentHashMap, ConcurrentSkipListMap, EnumMap, HashMap, Hashtable, IdentityHashMap, LinkedHashMap, PrinterStateReasons, Properties, Provider, RenderingHints, SimpleBindings, TabularDataSupport, TreeMap, UIDefaults, WeakHashMap
常用:HashMap类,LinkedHashMap类,TreeMap类不是同步方法,HashTable类是同步方法。
常用方法:
int size()//返回Map集合大小
boolean isEmpty()//判断Map集合是否为空(true为空)
boolean containsKey(Object key)//判断Map结合中是否含有Key(true包含)
boolean containsValue(Object value)//判读Map集合中是否含有Value(true包含)
V get(Object key)//返回Key得到对应的Value
V put(K key,V value)//把对应的Key—Value保存到Map集合中
void putAll(Map<? extends K,? extends V> m)//把另一个Map集合存到这个Map集合中
V remove(Object key)//把某一个对应的Key—Value移出Map集合,返回Value
void clear()//清空Map集合中的所有键值对对
Collection接口常用方法:
boolean add(E e)
确保此 collection 包含指定的元素。
boolean addAll(Collection<? extends E> c)
将指定 collection 中的所有元素都添加到此 collection 中。
void clear()
移除此 collection 中的所有元素。
boolean contains(Object o)
如果此 collection 包含指定的元素,则返回 true。
boolean containsAll(Collection<?> c)
如果此 collection 包含指定 collection 中的所有元素,则返回 true。
boolean isEmpty()
如果此 collection 不包含元素,则返回 true。
Iterator<E> iterator()
返回在此 collection 的元素上进行迭代的迭代器。(遍历)
boolean remove(Object o)
从此 collection 中移除指定元素的单个实例,如果存在的话。
boolean removeAll(Collection<?> c)
移除此 collection 中那些也包含在指定 collection 中的所有元素。
boolean retainAll(Collection<?> c)
仅保留此 collection 中那些也包含在指定 collection 的元素。
int size()
返回此 collection 中的元素数。
Object[] toArray()
返回包含此 collection 中所有元素的数组。
<T> T[] toArray(T[] a)
返回包含此 collection 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。
Set接口实现了Collection接口(无序可重复存储)——equals方法
已知实现子类
AbstractSet, ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, JobStateReasons, LinkedHashSet, TreeSet
常用:LinkedHashSet, TreeSet,HashSet类实现了Set接口,ConcurrentSkipListSet, CopyOnWriteArraySet是线程安全的,其他都不是线程安全的,需要自己处理。
List接口实现了Collection接口(有序不可重复存储)——equals方法
比较大小需要实现comparable接口,重写compareTo()方法,用来比较大小排序。
已知实现子类
AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector
常用:ArrayList类,LinkedList类不是同步方法,CopyOnWriteArrayList是同步方法。
容器类对象在调用remove,contains等方法时需要比较对象是否相等,涉及到equals方法和hashCode方法;对于用户自定义的类型,需要重写这两个方法。
Iterator接口(集合类都实现了该方法)
容器类具体实现遍历对象,用以统一遍历管理。
常用方法:
boolean hasNext()
//如果包含下一个待处理的数据对象(List、Map、Set)返回trueE next()//Returns the next element in the iteration.
//得到下一个待处理的数据对象(List、Map、Set)default void remove()
//移除某一个数据对象