`

Collection

 
阅读更多

Collection 定义了可装载元素的容器。

 

List,Set,Queue是它的子类。 

 

JAVA API 方法:

 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 equals(Object o)
          比较此 collection 与指定对象是否相等。
 int hashCode()
          返回此 collection 的哈希码值。
 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 中所有元素的数组;返回数组的运行时类型与指定数组的运行时类型相同。

 

 

Collection  的抽像子类:AbstractCollection 实现了上面的部分方法。()

AbstractCollection  是大部分容器实现类的父类, 它实现的方法具有一定通用性,子类就不用再实现了,实现了方法共用,如子类有不同实现,可以覆盖。 

 

AbstractCollection  的每一个子类者有各自的特性和数据结构,它是如何做到方法的通用性的:

抽像方法:

      public abstract Iterator<E> iterator();

      public abstract int size();

  AbstractCollection 不知道子类的具体实现算法,只能给出抽像方法,让子类去实现,  AbstractCollection 的其它方法都是基于这两个方法来实现的。

 

AbstractCollection 中add 方法抛出异常 : AbstractCollection 每一个子类都有不同的特性或数据结构,  AbstractCollection 中无法做到通的实现,抛出异常,需要由子类去实现。

(为什么不做成抽象方法? 或不实现 AbstractCollection 本身为抽像类不需要实现所有方法

 

 

Collections:

 Collections 针对Collection操作的静态工具类。

方法:

 
static
<T> boolean
addAll(Collection<? super T> c, T... elements)
          将所有指定元素添加到指定 collection 中。
static
<T> Queue<T>
asLifoQueue(Deque<T> deque)
          以后进先出 (Lifo) Queue 的形式返回某个 Deque 的视图。
static
<T> int
binarySearch(List<? extends Comparable<? super T>> list, T key)
          使用二分搜索法搜索指定列表,以获得指定对象。
static
<T> int
binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
          使用二分搜索法搜索指定列表,以获得指定对象。
static
<E> Collection<E>
checkedCollection(Collection<E> c, Class<E> type)
          返回指定 collection 的一个动态类型安全视图。
static
<E> List<E>
checkedList(List<E> list, Class<E> type)
          返回指定列表的一个动态类型安全视图。
static
<K,V> Map<K,V>
checkedMap(Map<K,V> m, Class<K> keyType, Class<V> valueType)
          返回指定映射的一个动态类型安全视图。
static
<E> Set<E>
checkedSet(Set<E> s, Class<E> type)
          返回指定 set 的一个动态类型安全视图。
static
<K,V> SortedMap<K,V>
checkedSortedMap(SortedMap<K,V> m, Class<K> keyType, Class<V> valueType)
          返回指定有序映射的一个动态类型安全视图。
static
<E> SortedSet<E>
checkedSortedSet(SortedSet<E> s, Class<E> type)
          返回指定有序 set 的一个动态类型安全视图。
static
<T> void
copy(List<? super T> dest, List<? extends T> src)
          将所有元素从一个列表复制到另一个列表。
static boolean disjoint(Collection<?> c1, Collection<?> c2)
          如果两个指定 collection 中没有相同的元素,则返回 true
static
<T> List<T>
emptyList()
          返回空的列表(不可变的)。
static
<K,V> Map<K,V>
emptyMap()
          返回空的映射(不可变的)。
static
<T> Set<T>
emptySet()
          返回空的 set(不可变的)。
static
<T> Enumeration<T>
enumeration(Collection<T> c)
          返回一个指定 collection 上的枚举。
static
<T> void
fill(List<? super T> list, T obj)
          使用指定元素替换指定列表中的所有元素。
static int frequency(Collection<?> c, Object o)
          返回指定 collection 中等于指定对象的元素数。
static int indexOfSubList(List<?> source, List<?> target)
          返回指定源列表中第一次出现指定目标列表的起始位置;如果没有出现这样的列表,则返回 -1。
static int lastIndexOfSubList(List<?> source, List<?> target)
          返回指定源列表中最后一次出现指定目标列表的起始位置;如果没有出现这样的列表,则返回 -1。
static
<T> ArrayList<T>
list(Enumeration<T> e)
          返回一个数组列表,它按返回顺序包含指定枚举返回的元素。
static

<T extends Object

& Comparable

<? super T>>
T

max(Collection<? extends T> coll)
          根据元素的自然顺序,返回给定 collection 的最大元素。
static
<T> T
max(Collection<? extends T> coll, Comparator<? super T> comp)
          根据指定比较器产生的顺序,返回给定 collection 的最大元素。
static

<T extends Object

& Comparable

<? super T>>
T

min(Collection<? extends T> coll)
          根据元素的自然顺序 返回给定 collection 的最小元素。
static
<T> T
min(Collection<? extends T> coll, Comparator<? super T> comp)
          根据指定比较器产生的顺序,返回给定 collection 的最小元素。
static
<T> List<T>
nCopies(int n, T o)
          返回由指定对象的 n 个副本组成的不可变列表。
static
<E> Set<E>
newSetFromMap(Map<E,Boolean> map)
          返回指定映射支持的 set。
static
<T> boolean
replaceAll(List<T> list, T oldVal, T newVal)
          使用另一个值替换列表中出现的所有某一指定值。
static void reverse(List<?> list)
          反转指定列表中元素的顺序。
static
<T> Comparator<T>
reverseOrder()
          返回一个比较器,它强行逆转实现了 Comparable 接口的对象 collection 的自然顺序
static
<T> Comparator<T>
reverseOrder(Comparator<T> cmp)
          返回一个比较器,它强行逆转指定比较器的顺序。
static void rotate(List<?> list, int distance)
          根据指定的距离轮换指定列表中的元素。
static void shuffle(List<?> list)
          使用默认随机源对指定列表进行置换。
static void shuffle(List<?> list, Random rnd)
          使用指定的随机源对指定列表进行置换。
static
<T> Set<T>
singleton(T o)
          返回一个只包含指定对象的不可变 set。
static
<T> List<T>
singletonList(T o)
          返回一个只包含指定对象的不可变列表。
static
<K,V> Map<K,V>
singletonMap(K key, V value)
          返回一个不可变的映射,它只将指定键映射到指定值。
static

<T extends

Comparable<? super T>>
void

sort(List<T> list)
          根据元素的自然顺序 对指定列表按升序进行排序。
static
<T> void
sort(List<T> list, Comparator<? super T> c)
          根据指定比较器产生的顺序对指定列表进行排序。
static void swap(List<?> list, int i, int j)
          在指定列表的指定位置处交换元素。
static
<T> Collection<T>
synchronizedCollection(Collection<T> c)
          返回指定 collection 支持的同步(线程安全的)collection。
static
<T> List<T>
synchronizedList(List<T> list)
          返回指定列表支持的同步(线程安全的)列表。
static
<K,V> Map<K,V>
synchronizedMap(Map<K,V> m)
          返回由指定映射支持的同步(线程安全的)映射。
static
<T> Set<T>
synchronizedSet(Set<T> s)
          返回指定 set 支持的同步(线程安全的)set。
static
<K,V> SortedMap<K,V>
synchronizedSortedMap(SortedMap<K,V> m)
          返回指定有序映射支持的同步(线程安全的)有序映射。
static
<T> SortedSet<T>
synchronizedSortedSet(SortedSet<T> s)
          返回指定有序 set 支持的同步(线程安全的)有序 set。
static
<T> Collection<T>
unmodifiableCollection(Collection<? extends T> c)
          返回指定 collection 的不可修改视图。
static
<T> List<T>
unmodifiableList(List<? extends T> list)
          返回指定列表的不可修改视图。
static
<K,V> Map<K,V>
unmodifiableMap(Map<? extends K,? extends V> m)
          返回指定映射的不可修改视图。
static
<T> Set<T>
unmodifiableSet(Set<? extends T> s)
          返回指定 set 的不可修改视图。
static
<K,V> SortedMap<K,V>
unmodifiableSortedMap(SortedMap<K,? extends V> m)
          返回指定有序映射的不可修改视图。
static
<T> SortedSet<T>

unmodifiableSortedSet(SortedSet<T> s)
          返回指定有序 set 的不可修改视图。

 

 

UnmodifiableXXX方法:使用代理模式,在修改操作方法上加上了 throw new UnsupportedOperationException()

 

SynchronizedXXX方法:使用代理模式,在所有的公用操作方法上加上同步锁。 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics