Collections工具类-java.util.Collections

Collections包含有各种有关集合操作的静态多态方法。此类不能实例化,就像一个工具类,服务于Java的Collection框架。

addAll(集合,值1,值2,值3...)
将一些元素添加到一个List中,此时我们一般有两种选择:Collections.addAll()或者是ArrayList.addAll()。在需添加元素比较少的情况下,并在List的size在万级以上时,一般建议Collections.addAll(),但当List的size较小时,两种方法没有什么区别,甚至ArrayList.addAll()更好。

 @SafeVarargs
    public static  boolean addAll(Collection c, T... elements) {
        boolean result = false;
        for (T element : elements)
            result |= c.add(element);
        return result;
    }

shuffle(List);
使用默认随机源对列表进行置换,所有置换发生的可能性都是大致相等的

   public static void shuffle(List list) {
        Random rnd = r;
        if (rnd == null)
            r = rnd = new Random(); // harmless race.
        shuffle(list, rnd);
    }

sort(List);
sort(List,比较器);

Collections中
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static  void sort(List list, Comparator c) {
        list.sort(c);
    }
Lists中
    @SuppressWarnings({"unchecked", "rawtypes"})
    default void sort(Comparator c) {
        Object[] a = this.toArray();
        Arrays.sort(a, (Comparator) c);
        ListIterator i = this.listIterator();
        for (Object e : a) {
            i.next();
            i.set((E) e);
        }
    }
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static  void sort(List list, Comparator c) {
        list.sort(c);
    }

binarySearch(List,目标值);
binarySearch(List,目标值,比较器);
此方法传入一个实现了Comparable接口的对象类的列表和要查找的元素。

    int binarySearch(List> list, T key) {
        if (list instanceof RandomAccess || list.size()
    int indexedBinarySearch(List> list, T key) {
        int low = 0;
        int high = list.size()-1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            Comparable midVal = list.get(mid);
            int cmp = midVal.compareTo(key);

            if (cmp < 0)
                low = mid + 1;
            else if (cmp > 0)
                high = mid - 1;
            else
                return mid; // key found
        }
        return -(low + 1);  // key not found
    }
 @SuppressWarnings("unchecked")
    public static  int binarySearch(List list, T key, Comparator c) {
        if (c==null)
            return binarySearch((List>) list, key);

        if (list instanceof RandomAccess || list.size() int indexedBinarySearch(List l, T key, Comparator c) {
        int low = 0;
        int high = l.size()-1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            T midVal = l.get(mid);
            int cmp = c.compare(midVal, key);

            if (cmp < 0)
                low = mid + 1;
            else if (cmp > 0)
                high = mid - 1;
            else
                return mid; // key found
        }
        return -(low + 1);  // key not found
    }

    private static  int iteratorBinarySearch(List l, T key, Comparator c) {
        int low = 0;
        int high = l.size()-1;
        ListIterator i = l.listIterator();

        while (low <= high) {
            int mid = (low + high) >>> 1;
            T midVal = get(i, mid);
            int cmp = c.compare(midVal, key);

            if (cmp < 0)
                low = mid + 1;
            else if (cmp > 0)
                high = mid - 1;
            else
                return mid; // key found
        }
        return -(low + 1);  // key not found
    }

Collections.swap(List,int,int);
交换集合两个元素的位置

    @SuppressWarnings({"rawtypes", "unchecked"})
    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)));
    }

你可能感兴趣的:(Collections工具类-java.util.Collections)