Collectionツール類-java.util.Collections

4737 ワード

Collectionは、集合動作に関する様々な静的多形法を含む。このような実用化はできません。ツール類のように、JavaのCollectionフレームワークにサービスを提供します。
addAll(集合、値1、値2、値3…)は、いくつかの要素を一つのListに追加する。この時、私たちは一般的に2つの選択がある。Collection.addAll()またはArayList.addAll()。要素を追加したい場合は、Listのsizeが万級以上の場合は、Collection.addAllが一般的に提案されていますが、Listのsizeが小さい場合は、2つの方法の違いはあまりなく、ArayList.addAll()も良いです。
 @SafeVarargs
    public static  boolean addAll(Collection super T> 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 super T> c) {
        list.sort(c);
    }
Lists 
    @SuppressWarnings({"unchecked", "rawtypes"})
    default void sort(Comparator super E> 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 super T> c) {
        list.sort(c);
    }
binarySearch(List,目標値)binarySearch(List,目標値,コンパレータ)この方法は、Comprableインターフェースを実現するオブジェクトクラスのリストと、検索する要素を入力します。
    int binarySearch(List extends Comparable super T>> list, T key) {
        if (list instanceof RandomAccess || list.size()
    int indexedBinarySearch(List extends Comparable super T>> list, T key) {
        int low = 0;
        int high = list.size()-1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            Comparable super T> 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 extends T> list, T key, Comparator super T> c) {
        if (c==null)
            return binarySearch((List extends Comparable super T>>) list, key);

        if (list instanceof RandomAccess || list.size() int indexedBinarySearch(List extends T> l, T key, Comparator super T> 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 extends T> l, T key, Comparator super T> c) {
        int low = 0;
        int high = l.size()-1;
        ListIterator extends T> 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
    }

Collection s.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)));
    }