Collections类主要是完成了两个主要功能 java
<T> boolean addAll(Collection<? super T> c, T... elements) //将全部指定元素添加到指定 collection 中。 <T> Queue<T> asLifoQueue(Deque<T> deque) //之后进先出 (Lifo) Queue 的形式返回某个 Deque 的视图。 <T> int binarySearch(List<? extends Comparable<? super T>> list, T key) //使用二分搜索法搜索指定列表,以得到指定对象。 <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c) //使用二分搜索法搜索指定列表,以得到指定对象。 <E> Collection<E> checkedCollection(Collection<E> c, Class<E> type) //返回指定 collection 的一个动态类型安全视图。 <E> List<E> checkedList(List<E> list, Class<E> type) //返回指定列表的一个动态类型安全视图。 <K,V> Map<K,V> checkedMap(Map<K,V> m, Class<K> keyType, Class<V> valueType) //返回指定映射的一个动态类型安全视图。 <E> Set<E> checkedSet(Set<E> s, Class<E> type) //返回指定 set 的一个动态类型安全视图。 <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K,V> m, Class<K> keyType, Class<V> valueType) //返回指定有序映射的一个动态类型安全视图。 <E> SortedSet<E> checkedSortedSet(SortedSet<E> s, Class<E> type) //返回指定有序 set 的一个动态类型安全视图。 <T> void copy(List<? super T> dest, List<? extends T> src) //将全部元素从一个列表复制到另外一个列表。
boolean disjoint(Collection<?> c1, Collection<?> c2) //若是两个指定 collection 中没有相同的元素,则返回 true。 <T> List<T> emptyList() //返回空的列表(不可变的)。 <K,V> Map<K,V> emptyMap() //返回空的映射(不可变的)。 <T> Set<T> emptySet() //返回空的 set(不可变的)。 <T> Enumeration<T> enumeration(Collection<T> c) //返回一个指定 collection 上的枚举。 <T> void fill(List<? super T> list, T obj) //使用指定元素替换指定列表中的全部元素。
int frequency(Collection<?> c, Object o) //返回指定 collection 中等于指定对象的元素数。
int indexOfSubList(List<?> source, List<?> target) //返回指定源列表中第一次出现指定目标列表的起始位置;若是没有出现这样的列表,则返回 -1。
int lastIndexOfSubList(List<?> source, List<?> target) //返回指定源列表中最后一次出现指定目标列表的起始位置;若是没有出现这样的列表,则返回 -1。 <T> ArrayList<T> list(Enumeration<T> e) //返回一个数组列表,它按返回顺序包含指定枚举返回的元素。 <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) //根据元素的天然顺序,返回给定 collection 的最大元素。 <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) //根据指定比较器产生的顺序,返回给定 collection 的最大元素。 <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) //根据元素的天然顺序 返回给定 collection 的最小元素。 <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) //根据指定比较器产生的顺序,返回给定 collection 的最小元素。 <T> List<T> nCopies(int n, T o) //返回由指定对象的 n 个副本组成的不可变列表。 <E> Set<E> newSetFromMap(Map<E,Boolean> map) //返回指定映射支持的 set。 <T> boolean replaceAll(List<T> list, T oldVal, T newVal) //使用另外一个值替换列表中出现的全部某一指定值。
void reverse(List<?> list) //反转指定列表中元素的顺序。 <T> Comparator<T> reverseOrder() //返回一个比较器,它强行逆转实现了 Comparable 接口的对象 collection 的天然顺序。 <T> Comparator<T> reverseOrder(Comparator<T> cmp) //返回一个比较器,它强行逆转指定比较器的顺序。
void rotate(List<?> list, int distance) //根据指定的距离轮换指定列表中的元素。
void shuffle(List<?> list) //使用默认随机源对指定列表进行置换。
void shuffle(List<?> list, Random rnd) //使用指定的随机源对指定列表进行置换。 <T> Set<T> singleton(T o) //返回一个只包含指定对象的不可变 set。 <T> List<T> singletonList(T o) //返回一个只包含指定对象的不可变列表。 <K,V> Map<K,V> singletonMap(K key, V value) //返回一个不可变的映射,它只将指定键映射到指定值。 <T extends Comparable<? super T>> void sort(List<T> list) //根据元素的天然顺序 对指定列表按升序进行排序。 <T> void sort(List<T> list, Comparator<? super T> c) //根据指定比较器产生的顺序对指定列表进行排序。
void swap(List<?> list, int i, int j) //在指定列表的指定位置处交换元素。 <T> Collection<T> synchronizedCollection(Collection<T> c) //返回指定 collection 支持的同步(线程安全的)collection。 <T> List<T> synchronizedList(List<T> list) //返回指定列表支持的同步(线程安全的)列表。 <K,V> Map<K,V> synchronizedMap(Map<K,V> m) //返回由指定映射支持的同步(线程安全的)映射。 <T> Set<T> synchronizedSet(Set<T> s) //返回指定 set 支持的同步(线程安全的)set。 <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m) //返回指定有序映射支持的同步(线程安全的)有序映射。 <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) //返回指定有序 set 支持的同步(线程安全的)有序 set。 <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) //返回指定 collection 的不可修改视图。 <T> List<T> unmodifiableList(List<? extends T> list) //返回指定列表的不可修改视图。 <K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m) //返回指定映射的不可修改视图。 <T> Set<T> unmodifiableSet(Set<? extends T> s) //返回指定 set 的不可修改视图。 <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K,? extends V> m) //返回指定有序映射的不可修改视图。 <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) //返回指定有序 set 的不可修改视图。
Arrays提供了一系列重载的sort方法,默认都是升序排列的。大致上能够分为两种,算法
一种是针对基本数据类型来进行排序,包括int,long,byte,float,double等类型,底层都是经过调用DualPivotQuicksort该类的sort方法来实现的。DualPivotQuicksort这个类是JAVA1.7以后专门提供给Java内部排序使用的专用类,被称为双枢轴快速排序,用来优化原先的快速排序,该算法通常能提供O(nlog(n))
的时间复杂度,数组
另外一种是针对Object数组类型来进行排序sort(Object[] a)。该算法的实现又分为两种,一种经过归并排序算法来实现,另外一种经过使用TimSort排序算法来实现。TimSort算法是一种插入与传统归并算法结合的一种算法,是对归并算法的一种优化。至于使用哪种算法,须要设置系统变量:java.util.Arrays.useLegacyMergeSort,经过设置为true,来使用归并算法,不然使用TimSort算法。默认状况下咱们是不会用到归并算法的,而且在JDK文档中有说明,在后续的版本中,legacyMergeSort归并算法会被移除掉。安全
Arrays一样提供了一系列重载的parallelSort方法,用于数字类型的并行排序,一样默认也是升序排列的。这一系列算法是JAVA1.8以后引入的,基于JAVA的并行处理框架fork/join框架,而fork/join框架,是Java1.7引入,目的是为了充分利用多核处理器,编写并行程序,提升程序性能的框架。
程序里进行了判断,若是数组长度过小,小于并行排序的最小长度或者并行线程池的大小是1,这时候就再也不使用并行处理,仍是使用DualPivotQuicksort的sort方法来进行排序。框架
Array.copy() 底层是经过System的native方法arraycopy来实现的,这个方法在操做集合的时候也常常用到。dom
System.CopyOf()性能
static <T> List<T> asList(T... a) //返回一个受指定数组支持的固定大小的列表。 static int binarySearch(byte[] a, byte key) //使用二分搜索法来搜索指定的 byte 型数组,以得到指定的值。 static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key) //使用二分搜索法来搜索指定的 byte 型数组的范围,以得到指定的值。 static int binarySearch(char[] a, char key) //使用二分搜索法来搜索指定的 char 型数组,以得到指定的值。 static int binarySearch(char[] a, int fromIndex, int toIndex, char key) //使用二分搜索法来搜索指定的 char 型数组的范围,以得到指定的值。 static int binarySearch(double[] a, double key) //使用二分搜索法来搜索指定的 double 型数组,以得到指定的值。 static int binarySearch(double[] a, int fromIndex, int toIndex, double key) //使用二分搜索法来搜索指定的 double 型数组的范围,以得到指定的值。 static int binarySearch(float[] a, float key) //使用二分搜索法来搜索指定的 float 型数组,以得到指定的值。 static int binarySearch(float[] a, int fromIndex, int toIndex, float key) //使用二分搜索法来搜索指定的 float 型数组的范围,以得到指定的值。 static int binarySearch(int[] a, int key) //使用二分搜索法来搜索指定的 int 型数组,以得到指定的值。 static int binarySearch(int[] a, int fromIndex, int toIndex, int key) //使用二分搜索法来搜索指定的 int 型数组的范围,以得到指定的值。 static int binarySearch(long[] a, int fromIndex, int toIndex, long key) //使用二分搜索法来搜索指定的 long 型数组的范围,以得到指定的值。 static int binarySearch(long[] a, long key) //使用二分搜索法来搜索指定的 long 型数组,以得到指定的值。 static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key) //使用二分搜索法来搜索指定数组的范围,以得到指定对象。 static int binarySearch(Object[] a, Object key) //使用二分搜索法来搜索指定数组,以得到指定对象。 static int binarySearch(short[] a, int fromIndex, int toIndex, short key) //使用二分搜索法来搜索指定的 short 型数组的范围,以得到指定的值。 static int binarySearch(short[] a, short key) //使用二分搜索法来搜索指定的 short 型数组,以得到指定的值。 static <T> int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) //使用二分搜索法来搜索指定数组的范围,以得到指定对象。 static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) //使用二分搜索法来搜索指定数组,以得到指定对象。 static boolean[] copyOf(boolean[] original, int newLength) //复制指定的数组,截取或用 false 填充(若有必要),以使副本具备指定的长度。 static byte[] copyOf(byte[] original, int newLength) //复制指定的数组,截取或用 0 填充(若有必要),以使副本具备指定的长度。 static char[] copyOf(char[] original, int newLength) //复制指定的数组,截取或用 null 字符填充(若有必要),以使副本具备指定的长度。 static double[] copyOf(double[] original, int newLength) //复制指定的数组,截取或用 0 填充(若有必要),以使副本具备指定的长度。 static float[] copyOf(float[] original, int newLength) //复制指定的数组,截取或用 0 填充(若有必要),以使副本具备指定的长度。 static int[] copyOf(int[] original, int newLength) //复制指定的数组,截取或用 0 填充(若有必要),以使副本具备指定的长度。 static long[] copyOf(long[] original, int newLength) //复制指定的数组,截取或用 0 填充(若有必要),以使副本具备指定的长度。 static short[] copyOf(short[] original, int newLength) //复制指定的数组,截取或用 0 填充(若有必要),以使副本具备指定的长度。 static <T> T[] copyOf(T[] original, int newLength) //复制指定的数组,截取或用 null 填充(若有必要),以使副本具备指定的长度。 static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) //复制指定的数组,截取或用 null 填充(若有必要),以使副本具备指定的长度。 static boolean[] copyOfRange(boolean[] original, int from, int to) //将指定数组的指定范围复制到一个新数组。 static byte[] copyOfRange(byte[] original, int from, int to) //将指定数组的指定范围复制到一个新数组。 static char[] copyOfRange(char[] original, int from, int to) //将指定数组的指定范围复制到一个新数组。 static double[] copyOfRange(double[] original, int from, int to) //将指定数组的指定范围复制到一个新数组。 static float[] copyOfRange(float[] original, int from, int to) //将指定数组的指定范围复制到一个新数组。 static int[] copyOfRange(int[] original, int from, int to) //将指定数组的指定范围复制到一个新数组。 static long[] copyOfRange(long[] original, int from, int to) //将指定数组的指定范围复制到一个新数组。 static short[] copyOfRange(short[] original, int from, int to) //将指定数组的指定范围复制到一个新数组。 static <T> T[] copyOfRange(T[] original, int from, int to) //将指定数组的指定范围复制到一个新数组。 static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) //将指定数组的指定范围复制到一个新数组。 static boolean deepEquals(Object[] a1, Object[] a2) //若是两个指定数组彼此是深层相等 的,则返回 true。 static int deepHashCode(Object[] a) //基于指定数组的“深层内容”返回哈希码。 static String deepToString(Object[] a) //返回指定数组“深层内容”的字符串表示形式。 static boolean equals(boolean[] a, boolean[] a2) //若是两个指定的 boolean 型数组彼此相等,则返回 true。 static boolean equals(byte[] a, byte[] a2) //若是两个指定的 byte 型数组彼此相等,则返回 true。 static boolean equals(char[] a, char[] a2) //若是两个指定的 char 型数组彼此相等,则返回 true。 static boolean equals(double[] a, double[] a2) //若是两个指定的 double 型数组彼此相等,则返回 true。 static boolean equals(float[] a, float[] a2) //若是两个指定的 float 型数组彼此相等,则返回 true。 static boolean equals(int[] a, int[] a2) //若是两个指定的 int 型数组彼此相等,则返回 true。 static boolean equals(long[] a, long[] a2) //若是两个指定的 long 型数组彼此相等,则返回 true。 static boolean equals(Object[] a, Object[] a2) //若是两个指定的 Objects 数组彼此相等,则返回 true。 static boolean equals(short[] a, short[] a2) //若是两个指定的 short 型数组彼此相等,则返回 true。 static void fill(boolean[] a, boolean val) //将指定的 boolean 值分配给指定 boolean 型数组的每一个元素。 static void fill(boolean[] a, int fromIndex, int toIndex, boolean val) //将指定的 boolean 值分配给指定 boolean 型数组指定范围中的每一个元素。 static void fill(byte[] a, byte val) //将指定的 byte 值分配给指定 byte 节型数组的每一个元素。 static void fill(byte[] a, int fromIndex, int toIndex, byte val) //将指定的 byte 值分配给指定 byte 型数组指定范围中的每一个元素。 static void fill(char[] a, char val) //将指定的 char 值分配给指定 char 型数组的每一个元素。 static void fill(char[] a, int fromIndex, int toIndex, char val) //将指定的 char 值分配给指定 char 型数组指定范围中的每一个元素。 static void fill(double[] a, double val) //将指定的 double 值分配给指定 double 型数组的每一个元素。 static void fill(double[] a, int fromIndex, int toIndex, double val) //将指定的 double 值分配给指定 double 型数组指定范围中的每一个元素。 static void fill(float[] a, float val) //将指定的 float 值分配给指定 float 型数组的每一个元素。 static void fill(float[] a, int fromIndex, int toIndex, float val) //将指定的 float 值分配给指定 float 型数组指定范围中的每一个元素。 static void fill(int[] a, int val) //将指定的 int 值分配给指定 int 型数组的每一个元素。 static void fill(int[] a, int fromIndex, int toIndex, int val) //将指定的 int 值分配给指定 int 型数组指定范围中的每一个元素。 static void fill(long[] a, int fromIndex, int toIndex, long val) //将指定的 long 值分配给指定 long 型数组指定范围中的每一个元素。 static void fill(long[] a, long val) //将指定的 long 值分配给指定 long 型数组的每一个元素。 static void fill(Object[] a, int fromIndex, int toIndex, Object val) //将指定的 Object 引用分配给指定 Object 数组指定范围中的每一个元素。 static void fill(Object[] a, Object val) //将指定的 Object 引用分配给指定 Object 数组的每一个元素。 static void fill(short[] a, int fromIndex, int toIndex, short val) //将指定的 short 值分配给指定 short 型数组指定范围中的每一个元素。 static void fill(short[] a, short val) //将指定的 short 值分配给指定 short 型数组的每一个元素。 static int hashCode(boolean[] a) //基于指定数组的内容返回哈希码。 static int hashCode(byte[] a) //基于指定数组的内容返回哈希码。 static int hashCode(char[] a) //基于指定数组的内容返回哈希码。 static int hashCode(double[] a) //基于指定数组的内容返回哈希码。 static int hashCode(float[] a) //基于指定数组的内容返回哈希码。 static int hashCode(int[] a) //基于指定数组的内容返回哈希码。 static int hashCode(long[] a) //基于指定数组的内容返回哈希码。 static int hashCode(Object[] a) //基于指定数组的内容返回哈希码。 static int hashCode(short[] a) //基于指定数组的内容返回哈希码。 static void sort(byte[] a) //对指定的 byte 型数组按数字升序进行排序。 static void sort(byte[] a, int fromIndex, int toIndex) //对指定 byte 型数组的指定范围按数字升序进行排序。 static void sort(char[] a) //对指定的 char 型数组按数字升序进行排序。 static void sort(char[] a, int fromIndex, int toIndex) //对指定 char 型数组的指定范围按数字升序进行排序。 static void sort(double[] a) //对指定的 double 型数组按数字升序进行排序。 static void sort(double[] a, int fromIndex, int toIndex) //对指定 double 型数组的指定范围按数字升序进行排序。 static void sort(float[] a) //对指定的 float 型数组按数字升序进行排序。 static void sort(float[] a, int fromIndex, int toIndex) //对指定 float 型数组的指定范围按数字升序进行排序。 static void sort(int[] a) //对指定的 int 型数组按数字升序进行排序。 static void sort(int[] a, int fromIndex, int toIndex) //对指定 int 型数组的指定范围按数字升序进行排序。 static void sort(long[] a) //对指定的 long 型数组按数字升序进行排序。 static void sort(long[] a, int fromIndex, int toIndex) //对指定 long 型数组的指定范围按数字升序进行排序。 static void sort(Object[] a) //根据元素的天然顺序对指定对象数组按升序进行排序。 static void sort(Object[] a, int fromIndex, int toIndex) //根据元素的天然顺序对指定对象数组的指定范围按升序进行排序。 static void sort(short[] a) //对指定的 short 型数组按数字升序进行排序。 static void sort(short[] a, int fromIndex, int toIndex) //对指定 short 型数组的指定范围按数字升序进行排序。 static <T> void sort(T[] a, Comparator<? super T> c) //根据指定比较器产生的顺序对指定对象数组进行排序。 static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) //根据指定比较器产生的顺序对指定对象数组的指定范围进行排序。 static String toString(boolean[] a) //返回指定数组内容的字符串表示形式。 static String toString(byte[] a) //返回指定数组内容的字符串表示形式。 static String toString(char[] a) //返回指定数组内容的字符串表示形式。 static String toString(double[] a) //返回指定数组内容的字符串表示形式。 static String toString(float[] a) //返回指定数组内容的字符串表示形式。 static String toString(int[] a) //返回指定数组内容的字符串表示形式。 static String toString(long[] a) //返回指定数组内容的字符串表示形式。 static String toString(Object[] a) //返回指定数组内容的字符串表示形式。 static String toString(short[] a) //返回指定数组内容的字符串表示形式。