Collections

[TOC]

通用算法

改变元素顺序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void reverse(List<?> list)
// reverse the order of the elements
void rotate(List<?> list, int distance)
// rotate the elements of the list; the element at index
// i is moved to index (distance + i) % list.size()
void shuffle(List<?> list)
// randomly permute the list elements
void shuffle(List<?> list, Random rnd)
// randomly permute the list using the randomness source rnd
<T extends Comparable<? super T>> void sort(List<T> list)
// sort the supplied list using natural ordering
<T> void sort(List<T> list, Comparator<? super T> c)
// sort the supplied list using the supplied ordering
void swap(List<?> list, int i, int j)
// swap the elements at the specified positions

改变元素内容

1
2
3
4
5
6
<T> void copy(List<? super T> dest, List<? extends T> src)
// copy all of the elements from one list into another
<T> void fill(List<? super T> list, T obj)
// replace every element of list with obj
<T> boolean replaceAll(List<T> list, T oldVal, T newVal)
// replace all occurrences of oldVal in list with newVal

在集合中找到极值

1
2
3
4
5
6
7
8
9
10
11
12
<T extends Object & Comparable<? super T>>
T max(Collection<? extends T> coll) // return the maximum element
// using natural ordering
<T> T max(Collection<? extends T> coll, Comparator<? super T> comp)
// return the maximum element
// using the supplied comparator
<T extends Object & Comparable<? super T>>
T min(Collection<? extends T> coll) // return the minimum element
// using natural ordering
<T> T min(Collection<? extends T> coll, Comparator<? super T> comp)
// return the minimum element
// using the supplied comparator

在集合中找到特殊的元素

1
2
3
4
5
6
7
8
<T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
// search for key using binary search
<T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
// search for key using binary search
int indexOfSubList(List<?> source, List<?> target)
// find the first sublist of source which matches target
int lastIndexOfSubList(List<?> source, List<?> target)
// find the last sublist of source which matches target

集合工厂

返回空值,不可修改(用于查询的时候返回空值用)

1
2
3
<T> List<T> emptyList() // return the empty list (immutable)
<K,V> Map<K,V> emptyMap() // return the empty map (immutable)
<T> Set<T> emptySet() // return the empty set (immutable)

返回一个不可修改的单例

1
2
3
4
5
6
<T> Set<T> singleton(T o)
// return an immutable set containing only the specified object
<T> List<T> singletonList(T o)
// return an immutable list containing only the specified object
<K,V> Map<K,V> singletonMap(K key, V value)
// return an immutable map, mapping only the key K to the value V

放回n个给定对象的引用(因为都是引用,所以节约空间)

1
2
<T> List<T> nCopies(int n, T o)
// return an immutable list containing n references to the object o

包装

Synchoronized Collections

用于少量同步

1
2
3
4
5
6
<T> Collection<T> synchronizedCollection(Collection<T> c);
<T> Set<T> synchronizedSet(Set<T> s);
<T> List<T> synchronizedList(List<T> list);
<K, V> Map<K, V> synchronizedMap(Map<K, V> m);
<T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s);
<K, V> SortedMap<K, V> synchronizedSortedMap(SortedMap<K, V> m);

Unmodifiable Collections

用于传递视图、通知等

1
2
3
4
5
6
<T> Collection<T> unmodifiableCollection(Collection<? extends T> c)
<T> Set<T> unmodifiableSet(Set<? extends T> s)
<T> List<T> unmodifiableList(List<? extends T> list)
<K, V> Map<K, V> unmodifiableMap(Map<? extends K, ? extends V> m)
<T> SortedSet<T> unmodifiableSortedSet(SortedSet<? extends T> s)
<K, V> SortedMap<K, V> unmodifiableSortedMap(SortedMap<K, ? extends V> m)

Checked Collections

用于和老代码交互时,操作集合需要检查类型(1.5前没泛型)

1
2
3
4
5
6
7
8
9
10
11
12
static <E> Collection
checkedCollection(Collection<E> c, Class<E> elementType)
static <E> List
checkedList(List<E> c, Class<E> elementType)
static <E> Set
checkedSet(Set<E> c, Class<E> elementType)
static <E> SortedSet
checkedSortedSet(SortedSet<E> c, Class<E> elementType)
static <K, V> Map
checkedMap(Map<K, V> c, Class<K> keyType, Class<V> valueType)
static <K, V> SortedMap
checkedSortedMap(SortedMap<K, V> c, Class<K> keyType,Class<V> valueType)

其他方法

参考:http://gitbook.net/java/util/java_util_collections.html

addAll

这个方法会将所有指定的元素到指定的集合

1
2
<T> boolean addAll(Collection<? super T> c, T... elements)
// adds all of the specified elements to the specified collection.

asLifoQueue

此方法返回一个deque的视图,作为一个后进先出(LIFO)队列

1
2
<T> Queue<T> asLifoQueue(Deque<T> deque)
// returns a view of a Deque as a Last-in-first-out (Lifo) Queue

disjoint

如果两个指定collection中没有相同的元素此方法返回true

1
2
boolean disjoint(Collection<?> c1, Collection<?> c2)
// returns true if c1 and c2 have no elements in common

enumeration

此方法返回一个枚举在指定的collection

1
2
<T> Enumeration<T> enumeration(Collection<T> c)
// returns an enumeration over the specified collection

frequency

此方法返回指定元素集合等于指定对象的数量。

1
2
int frequency(Collection<?> c, Object o)
// returns the number of elements in c that are equal to o

list

用于老类型转新类型

1
2
<T> ArrayList<T> list(Enumeration<T> e)
// returns an ArrayList containing the elements returned by the specified Enumeration

用法:

1
2
3
4
5
6
7
8
9
10
11

public static void main(String[] args) {
Vector<String> v = new Vector<String>();
v.add("A");
v.add("B");
System.out.println(v);
Enumeration<String> e = v.elements();

ArrayList<String> aList = Collections.list(e);
System.out.println(aList);
}

newSetFromMap

通过map创建set

1
2
<E> Set<E> newSetFromMap(Map<E, Boolean> map)
// returns a set backed by the specified map

reverseOrder

1
2
<T> Comparator<T> reverseOrder()
// returns a comparator that reverses natural ordering

用法例子:

1
2
3
SortedSet<Integer> s = new TreeSet<Integer>(Collections.reverseOrder());
Collections.addAll(s, 1, 2, 3);
assert s.toString().equals("[3, 2, 1]");
0%