Collections常用API及其解释
1.reverse
public static void reverse(List<?> list);
用法:反转list
2.sort
public static <T> void sort(List<T> list, Comparator<? super T> c) {
list.sort(c);
}
排序,高级用法是,传入一个lambda表达式,对其进行排序。
此处,如果前一个元素compareTo后一个元素的结果大于0(同时满足其他条件的情况下),需要对前一个元素和后一个元素进行换位,也就是说,当compareTo的返回值大于0的时候,进行元素的移位,其他情况,不作处理。
3.swap
public static void swap(List<?> list, int i, int j) {
// instead of using a raw type here, it's possible to capture
// the wildcard but it will require a call to a supplementary
// private method
final List l = list;
l.set(i, l.set(j, l.get(i)));
}
/**
* Swaps the two specified elements in the specified array.
*/
private static void swap(Object[] arr, int i, int j) {
Object tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
交换指定元素,有两种方式
4.min()和max()
此处是求最大最小值
public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) {
Iterator<? extends T> i = coll.iterator();
T candidate = i.next();
while (i.hasNext()) {
T next = i.next();
if (next.compareTo(candidate) < 0)
candidate = next;
}
return candidate;
}
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) {
Iterator<? extends T> i = coll.iterator();
T candidate = i.next();
while (i.hasNext()) {
T next = i.next();
if (next.compareTo(candidate) > 0)
candidate = next;
}
return candidate;
}
5.copy()
复制元素到新list里面
public static <T> void copy(List<? super T> dest, List<? extends T> src) {
int srcSize = src.size();
if (srcSize > dest.size())
throw new IndexOutOfBoundsException("Source does not fit in dest");
if (srcSize < COPY_THRESHOLD ||
(src instanceof RandomAccess && dest instanceof RandomAccess)) {
for (int i=0; i<srcSize; i++)
dest.set(i, src.get(i));
} else {
ListIterator<? super T> di=dest.listIterator();
ListIterator<? extends T> si=src.listIterator();
for (int i=0; i<srcSize; i++) {
di.next();
di.set(si.next());
}
}
}
6.fill()
填充list中的元素
public static <T> void fill(List<? super T> list, T obj) {
int size = list.size();
if (size < FILL_THRESHOLD || list instanceof RandomAccess) {
for (int i=0; i<size; i++)
list.set(i, obj);
} else {
ListIterator<? super T> itr = list.listIterator();
for (int i=0; i<size; i++) {
itr.next();
itr.set(obj);
}
}
}
7.replaceAll()
用作数组替换
替换值存在,返回True,否则返回false
public static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) {
boolean result = false;
int size = list.size();
if (size < REPLACEALL_THRESHOLD || list instanceof RandomAccess) {
if (oldVal==null) {
for (int i=0; i<size; i++) {
if (list.get(i)==null) {
list.set(i, newVal);
result = true;
}
}
} else {
for (int i=0; i<size; i++) {
if (oldVal.equals(list.get(i))) {
list.set(i, newVal);
result = true;
}
}
}
} else {
ListIterator<T> itr=list.listIterator();
if (oldVal==null) {
for (int i=0; i<size; i++) {
if (itr.next()==null) {
itr.set(newVal);
result = true;
}
}
} else {
for (int i=0; i<size; i++) {
if (oldVal.equals(itr.next())) {
itr.set(newVal);
result = true;
}
}
}
}
return result;
}
8.shuffle()
使用默认的随机源指定排列列表
public static void shuffle(List<?> list) {
Random rnd = r;
if (rnd == null)
r = rnd = new Random(); // harmless race.
shuffle(list, rnd);
}
9.rotate()
将数组往后面旋转distance个位置
public static void rotate(List<?> list, int distance) {
if (list instanceof RandomAccess || list.size() < ROTATE_THRESHOLD)
rotate1(list, distance);
else
rotate2(list, distance);
}
10.indexOfSubList()和lastIndexOfSubList()
这里是返回第一次和最后一次出现的起始位置,如果没有则返回-1
public static int indexOfSubList(List<?> source, List<?> target) {
int sourceSize = source.size();
int targetSize = target.size();
int maxCandidate = sourceSize - targetSize;
if (sourceSize < INDEXOFSUBLIST_THRESHOLD ||
(source instanceof RandomAccess&&target instanceof RandomAccess)) {
nextCand:
for (int candidate = 0; candidate <= maxCandidate; candidate++) {
for (int i=0, j=candidate; i<targetSize; i++, j++)
if (!eq(target.get(i), source.get(j)))
continue nextCand; // Element mismatch, try next cand
return candidate; // All elements of candidate matched target
}
} else { // Iterator version of above algorithm
ListIterator<?> si = source.listIterator();
nextCand:
for (int candidate = 0; candidate <= maxCandidate; candidate++) {
ListIterator<?> ti = target.listIterator();
for (int i=0; i<targetSize; i++) {
if (!eq(ti.next(), si.next())) {
// Back up source iterator to next candidate
for (int j=0; j<i; j++)
si.previous();
continue nextCand;
}
}
return candidate;
}
}
return -1; // No candidate matched the target
}
/**
* Returns the starting position of the last occurrence of the specified
* target list within the specified source list, or -1 if there is no such
* occurrence. More formally, returns the highest index {@code i}
* such that {@code source.subList(i, i+target.size()).equals(target)},
* or -1 if there is no such index. (Returns -1 if
* {@code target.size() > source.size()})
*
* <p>This implementation uses the "brute force" technique of iterating
* over the source list, looking for a match with the target at each
* location in turn.
*
* @param source the list in which to search for the last occurrence
* of {@code target}.
* @param target the list to search for as a subList of {@code source}.
* @return the starting position of the last occurrence of the specified
* target list within the specified source list, or -1 if there
* is no such occurrence.
* @since 1.4
*/
public static int lastIndexOfSubList(List<?> source, List<?> target) {
int sourceSize = source.size();
int targetSize = target.size();
int maxCandidate = sourceSize - targetSize;
if (sourceSize < INDEXOFSUBLIST_THRESHOLD ||
source instanceof RandomAccess) { // Index access version
nextCand:
for (int candidate = maxCandidate; candidate >= 0; candidate--) {
for (int i=0, j=candidate; i<targetSize; i++, j++)
if (!eq(target.get(i), source.get(j)))
continue nextCand; // Element mismatch, try next cand
return candidate; // All elements of candidate matched target
}
} else { // Iterator version of above algorithm
if (maxCandidate < 0)
return -1;
ListIterator<?> si = source.listIterator(maxCandidate);
nextCand:
for (int candidate = maxCandidate; candidate >= 0; candidate--) {
ListIterator<?> ti = target.listIterator();
for (int i=0; i<targetSize; i++) {
if (!eq(ti.next(), si.next())) {
if (candidate != 0) {
// Back up source iterator to next candidate
for (int j=0; j<=i+1; j++)
si.previous();
}
continue nextCand;
}
}
return candidate;
}
}
return -1; // No candidate matched the target
}
11.unmodifiableXxx()
返回Xxx不可变视图:
举例:Collection:
// Unmodifiable Wrappers
/**
* Returns an <a href="Collection.html#unmodview">unmodifiable view</a> of the
* specified collection. Query operations on the returned collection "read through"
* to the specified collection, and attempts to modify the returned
* collection, whether direct or via its iterator, result in an
* {@code UnsupportedOperationException}.<p>
*
* The returned collection does <i>not</i> pass the hashCode and equals
* operations through to the backing collection, but relies on
* {@code Object}'s {@code equals} and {@code hashCode} methods. This
* is necessary to preserve the contracts of these operations in the case
* that the backing collection is a set or a list.<p>
*
* The returned collection will be serializable if the specified collection
* is serializable.
*
* @param <T> the class of the objects in the collection
* @param c the collection for which an unmodifiable view is to be
* returned.
* @return an unmodifiable view of the specified collection.
*/
public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) {
return new UnmodifiableCollection<>(c);
}
Arrays类常用API及其解释
1.asList()
public static <T> List<T> asList(T... a)
将array或一组不定长数据转换成List
2.binarySearch()
public static <T> int binarySearch(T[] a,
int fromIndex,
int toIndex,
T key,
Comparator<? super T> c)
public static <T> int binarySearch(T[] a,
T key,
Comparator<? super T> c)
下面这种,一般要实现equals()方法,但是如果是基础数据类型,可以不用实现。
public static int binarySearch(Object[] a,
Object key)
一共三种写好的二分查找方法。
3.fill()方法
public static void fill(Object[] a,
Object val)
public static void fill(Object[] a,
int fromIndex,
int toIndex,
Object val)
4.sort()方法
public static <T> void sort(T[] a,
Comparator<? super T> c)
public static <T> void sort(T[] a,
int fromIndex,
int toIndex,
Comparator<? super T> c)
public static void sort(Object[] a,
int fromIndex,
int toIndex)
public static void sort(Object[] a)
sort方法的所有形式。
此处,如果前一个元素compareTo后一个元素的结果大于0(同时满足其他条件的情况下),需要对前一个元素和后一个元素进行换位,也就是说,当compareTo的返回值大于0的时候,进行元素的移位,其他情况,不作处理。
5.toString()
public static String toString(Object[] a)
6.copyOf()、copyOfRange()
public static <T,U> T[] copyOf(U[] original,
int newLength,
Class<? extends T[]> newType)
public static <T> T[] copyOf(T[] original,
int newLength)
public static <T> T[] copyOfRange(T[] original,
int from,
int to)
public static <T,U> T[] copyOfRange(U[] original,
int from,
int to,
Class<? extends T[]> newType)
7.equals()
public static boolean equals(Object[] a,
Object[] a2)
以上就是所有Arrays类的基本常用用法,具体可查Java文档。