Arrays
提供操作数组的方法
成员变量
private static final int INSERTIONSORT_THRESHOLD = 7;
private static final int MIN_ARRAY_SORT_GRAN = 1 << 13;
重要方法
二分搜索数组
使用二分法搜索指定key
//搜索指定范围
public static int binarySearch(int[] a, int fromIndex, int toIndex,int key)
······
复制指定范围的数组
本质调用System.arraycopy()方法
//复制数组的指定范围
public static <T> T[] copyOfRange(T[] original, int from, int to)
······
使用指定元素填充数组
//填充数组[fromIndex,toIndex)范围内为val
public static void fill(int[] a, int fromIndex, int toIndex, int val)
······
判断两个数组是否相等
引用->是否为null->长度->数组中元素
public static boolean equals(int[] a, int[] a2)
······
对数组中的元素进行排序
底层使用快排算法,小于47使用插入排序,大于286使用归并排序
public static void sort(int[] a)
······
Collections
提供对集合进行操作的多态算法,单例
成员变量
public static final Map EMPTY_MAP = new EmptyMap<>();
public static final List EMPTY_LIST = new EmptyList<>();
public static final Set EMPTY_SET = new EmptySet<>();
private static Random r;
//以下值代表了该操作使用随机访问的数据的阈值(获得很好性能)
private static final int BINARYSEARCH_THRESHOLD = 5000;
private static final int REVERSE_THRESHOLD = 18;
private static final int SHUFFLE_THRESHOLD = 5;
private static final int FILL_THRESHOLD = 25;
private static final int ROTATE_THRESHOLD = 100;
private static final int COPY_THRESHOLD = 10;
private static final int REPLACEALL_THRESHOLD = 11;
private static final int INDEXOFSUBLIST_THRESHOLD = 35;
重要方法
返回线程同步集合
将当前包装对象作为锁来同步处理
public static <T> Collection<T> synchronizedCollection(Collection<T> c)
public static <T> Set<T> synchronizedSet(Set<T> s)
public static <T> List<T> synchronizedList(List<T> list)
public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m)
返回不可变集合
对于增删改操作直接抛出异常
public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c)
public static <T> Set<T> unmodifiableSet(Set<? extends T> s)
public static <T> List<T> unmodifiableList(List<? extends T> list)
public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m)
返回只包含一个元素的不可变集合
包含指定对象的不可变集
public static <T> Set<T> singleton(T o)
public static <K,V> Map<K,V> singletonMap(K key, V value)
·······
返回指定集合的动态类型安全视图
添加修改的元素必须为指定的动态类型
public static <E> List<E> checkedList(List<E> list, Class<E> type)
public static <K, V> Map<K, V> checkedMap(Map<K, V> m,
Class<K> keyType,
Class<V> valueType)
返回集合之间转换后的结果
//给定一个Enumeration类型返回ArrayList
public static <T> ArrayList<T> list(Enumeration<T> e)
对集合中元素的操作
addAll(Collection<? super T> c, T… elements)
//将指定数组集合添加到Collection集合中
public static <T> boolean addAll(Collection<? super T> c, T... elements) {
boolean result = false;
for (T element : elements)
result |= c.add(element);
return result;
}
copy(List<? super T> dest, List<? extends T> src)
将源List中的元素复制到目标dest,如果dest长度小于src长度抛出异常
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());
}
}
}
sort()
本质上是调用List集合自身的sort方法,1.转换为数组,2.Arrays.sort排序数组,3遍历调用set重新赋值
public static <T extends Comparable<? super T>> void sort(List<T> list) {
list.sort(null);
}
swap(List<?> list, int i, int j)
交换集合中两个元素的位置
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)));
}
reverse(List<?> list)
颠倒集合中元素
public static void reverse(List<?> list) {
int size = list.size();
if (size < REVERSE_THRESHOLD || list instanceof RandomAccess) {
for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--)
swap(list, i, j);
} else {
// 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
ListIterator fwd = list.listIterator();
ListIterator rev = list.listIterator(size);
for (int i=0, mid=list.size()>>1; i<mid; i++) {
Object tmp = fwd.next();
fwd.set(rev.previous());
rev.set(tmp);
}
}
}
rotate(List<?> list, int distance)
旋转集合元素,distance为正右移位数,为负左移位数
public static void rotate(List<?> list, int distance) {
if (list instanceof RandomAccess || list.size() < ROTATE_THRESHOLD)
rotate1(list, distance);
else
rotate2(list, distance);
}
disjoint(Collection<?> c1, Collection<?> c2)
判断集合是否有共同元素,没有返回true
public static boolean disjoint(Collection<?> c1, Collection<?> c2) {
Collection<?> contains = c2;
Collection<?> iterate = c1;
if (c1 instanceof Set) {
iterate = c2;
contains = c1;
} else if (!(c2 instanceof Set)) {
int c1size = c1.size();
int c2size = c2.size();
if (c1size == 0 || c2size == 0) {
return true;
}
if (c1size > c2size) {
iterate = c2;
contains = c1;
}
}
for (Object e : iterate) {
if (contains.contains(e)) {
return false;
}
}
return true;
}
max(Collection<? extends T> coll)
寻找集合中最大的元素,元素必须继承自Comparable接口
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;
}