Arrays 封装了对数组操作的类,主要方法有:
1.1 asList
//将数组转化为List
static <T> List<T> asList(T... a)
1.2 binarySearch
//在数组中查找key,使用的是二分查找.成功返回下标,失败返回负数
static int binarySearch(byte[] a, byte key)
//在指定范围内查找key
static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key)
1.3 copyOf
//对数组的快速复制
static boolean[] copyOf(boolean[] original, int newLength)
//复制一段数组
static boolean[] copyOfRange(boolean[] original, int from, int to)
1.4 fill
//填充数组的值
static void fill(int[] a, int val)
static void fill(int[] a, int fromIndex, int toIndex, int val)
1.5 equals
//两个数组进行比较,如果两个数组长度相同、内部值相同返回true
static boolean equals(int[] a, int[] a2)
1.6 sort
//排序 1.7之前使用的是普通的快排。现在使用的是双轴快排
static void sort(byte[] a)
static void sort(byte[] a, int fromIndex, int toIndex)
1.7 toString
static String toString(int[] a)
另外的一些大多是跟lamda操作相关。Arrays提供的都是static方法,可以方便使用
2、Collections
Collections主要封装了一些集合类的静态操作,主要有:
//二分法查找集合中的元素
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)
//添加对象时,会先检查该对象是否是type类型,不是则抛ClassCastException
static <E> Collection<E> checkedCollection/List/Map(Collection<E> c, Class<E> type)
//判断两个集合是否有交集
static boolean disjoint(Collection<?> c1, Collection<?> c2)
//倒序
static void reverse(List<?> list)
//排序
static <T extends Comparable<? super T>>
void sort(List<T> list)
//返回集合c中包含元素o的个数
static int frequency(Collection<?> c, Object o)
//返回一个线程安全的集合
static <T> Collection<T> synchronizedCollection(Collection<T> c)
//返回一个不可变的集合,即内部元素只能查不能修改
static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c)