天天看點

Collections集合工具的總結

Collections是一個操作List,Set和Map集合的工具類:

增加元素:

static <T> boolean addAll(Collection<? super T> c, T... elements) 将所有指定的元素添加到指定的集合。

List<Integer> list = new ArrayList<>();
Collections.addAll(list,3,5,2,5,6,9,10,-1,6);//9個元素      

對List集合進行排序:

void reverse(List list) 反序

void shuffle(List list) 亂序

void sort(List list) 按照元素的自然順序進行排序

void sort(List list,Comparator) 按照比較器定義的規則進行排序

void swap(List list,int i,int j) 将list集合中第i個和第j個元素互換

void rotate(list,int distance) 當distance為正數時,将list集合的後distance個元素整體移到前面.當distance為負數時,将list集合的前distance個元素整體移到後面.不會改變集合的大小

/**
 * Collections集合工具對list集合的排序方法測試
 */
private static void method1() {
    List<Integer> list = new ArrayList<>();
    Collections.addAll(list,3,5,2,5,6,9,10,-1,6);//9個元素
    Collections.sort(list);
    System.out.println(list);//[-1, 2, 3, 5, 5, 6, 6, 9, 10]
    Collections.sort(list,(i,j)->{
        return j-i;//從大到小排序
    });
    System.out.println(list);//[10, 9, 6, 6, 5, 5, 3, 2, -1]
    Collections.swap(list,0,1);
    System.out.println(list);//[9, 10, 6, 6, 5, 5, 3, 2, -1]
    Collections.rotate(list,8);
    System.out.println(list);//[10, 6, 6, 5, 5, 3, 2, -1, 9]
}      

查找元素:

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) 使用二叉搜尋算法搜尋指定對象的指定清單。 Comparator為null,則按照自然順序進行排序

static <T extends Object & Comparable<? super T>>T max(Collection<? extends T> coll) 根據其元素的 自然順序傳回給定集合的最大元素。

static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) 根據指定的比較器定義的順序傳回給定集合的最大元素。

static <T extends Object & Comparable<? super T>>T min(Collection<? extends T> coll) 根據其元素的 自然順序傳回給定集合的最小元素。

static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) 根據指定的比較器引發的順序傳回給定集合的最小元素。

static int frequency(Collection<?> c, Object o) 傳回指定集合中與指定對象相等的元素數。

static int indexOfSubList(List<?> source, List<?> target) 傳回指定源清單中指定目标清單的第一次出現的起始位置,如果沒有此類事件,則傳回-1。

static int lastIndexOfSubList(List<?> source, List<?> target) 傳回指定源清單中指定目标清單的最後一次出現的起始位置,如果沒有此類事件則傳回-1。

替換:

static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) 将清單中一個指定值的所有出現替換為另一個。

static <T> void fill(List<? super T> list, T obj) 用指定的元素代替指定清單的所有元素。

/**
 * Collections對集合元素的查找和替換
 */
private static void method2() {
    List<String> list = new ArrayList<>();
    Collections.addAll(list,"a","aa","aa","a","aaa","a","aaaa","aaa","aaaaa");
    System.out.println(Collections.binarySearch(list,"a"));//0,未排序,不保證搜尋的結果
    System.out.println();
    Collections.sort(list);//自然排序後搜尋
    System.out.println(list);//[a, a, a, aa, aa, aaa, aaa, aaaa, aaaaa]
    System.out.println(Collections.binarySearch(list,"a"));//1
    System.out.println(Collections.binarySearch(list,"a",(s1,s2)->{
        return s2.length()-s1.length();
    }));//-10 搜尋不到
    System.out.println(Collections.binarySearch(list,"a",null));//1
    System.out.println(Collections.max(list));//aaaaa
    Comparator<String> comparator = (s1, s2)->{ return s2.length()-s1.length();};
    System.out.println(Collections.max(list,comparator));//a
    //自定義排序後搜尋
    Collections.sort(list,comparator);
    System.out.println(list);//[aaaaa, aaaa, aaa, aaa, aa, aa, a, a, a]
    System.out.println(Collections.binarySearch(list,"a"));//-1 搜尋不到
    System.out.println(Collections.binarySearch(list,"a",comparator));//6
    System.out.println(Collections.max(list));//aaaaa
    System.out.println(Collections.max(list,(s1,s2)->{//aaaaa
        return s1.length()-s2.length();
    }));
    System.out.println(Collections.max(list,comparator));//a

    System.out.println(Collections.frequency(list,"a"));//3,出現了3次
    List<String> list2 = new ArrayList<>();
    Collections.addAll(list2,"aaaa","aaa");
    System.out.println(Collections.indexOfSubList(list,list2));//1
    list2.clear();
    Collections.addAll(list2,"aaaa","aa");
    System.out.println(Collections.indexOfSubList(list,list2));//-1,兩個集合中元素的順序要一緻才認為是子集合

    Collections.replaceAll(list,"a","b");
    System.out.println(list);//[aaaaa, aaaa, aaa, aaa, aa, aa, b, b, b]
    Collections.fill(list,"zz");
    System.out.println(list);//[zz, zz, zz, zz, zz, zz, zz, zz, zz]
}      

線程安全:

Collections提供了synchoronizedXxx方法,這些方法可以将對應的集合包裝成線程同步的集合,進而解決線程安全問題.

ArrayList,LinkedList,ArrayDeque,HashSet,LinkedHashSet,TreeSet,HashMap,LinkedHashMap,TreeMap都是線程不安全的.如果有多個線程通路線程不安全的集合,并且有一個以上的線程要修改該結合,那麼就存線上程安全問題.Collections的同步方法就是用來解決這個問題的.

static <T> Collection<T> synchronizedCollection(Collection<T> c) 傳回由指定集合支援的同步(線程安全)集合。

static <T> List<T> synchronizedList(List<T> list) 傳回由指定清單支援的同步(線程安全)清單。

static <T> Set<T> synchronizedSet(Set<T> s) 傳回由指定集合支援的同步(線程安全)集。

static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) 傳回由指定地圖支援的同步(線程安全)映射。

static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) 傳回由指定的排序集支援的同步(線程安全)排序集。

static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m) 傳回由指定的排序映射支援的同步(線程安全)排序映射。

static <T> NavigableSet<T> synchronizedNavigableSet(NavigableSet<T> s) 傳回由指定的可導航集支援的同步(線程安全)可導航集。

static <K,V> NavigableMap<K,V> synchronizedNavigableMap(NavigableMap<K,V> m) 傳回由指定的可導航地圖支援的同步(線程安全)可導航地圖。

/**
 * 同步方法
 */
private static void mehtod3() {
    List list = Collections.synchronizedList(new ArrayList<>());
    Collection queue = Collections.synchronizedCollection(new ArrayDeque<>());
    Set set = Collections.synchronizedSet(new HashSet());
    SortedSet sset = Collections.synchronizedSortedSet(new TreeSet<>());
    Map map = Collections.synchronizedMap(new HashMap<>());
    SortedMap smap = Collections.synchronizedSortedMap(new TreeMap<>());
}      

設定不可變集合:

0個元素

static <T> List<T> emptyList() 傳回空清單(immutable)。

static <T> ListIterator<T> emptyListIterator() 傳回沒有元素的清單疊代器。

static <K,V> Map<K,V> emptyMap() 傳回空的地圖(不可變)。

static <K,V> NavigableMap<K,V> emptyNavigableMap() 傳回空導航地圖(不可變)。

static <E> NavigableSet<E> emptyNavigableSet() 傳回一個空導航集(immutable)。

static <T> Set<T> emptySet() 傳回一個空集(immutable)。

static <K,V> SortedMap<K,V> emptySortedMap() 傳回空的排序映射(immutable)。

static <E> SortedSet<E> emptySortedSet() 傳回一個空的排序集(immutable)。

1個元素

static <T> Set<T> singleton(T o) 傳回一個隻包含指定對象的不可變集。

static <T> List<T> singletonList(T o) 傳回一個隻包含指定對象的不可變清單。

static <K,V> Map<K,V> singletonMap(K key, V value) 傳回一個不可變的地圖,隻将指定的鍵映射到指定的值。

多個個元素

static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) 傳回指定集合的不可修改視圖。

static <T> List<T> unmodifiableList(List<? extends T> list) 傳回指定清單的不可修改視圖。

static <T> Set<T> unmodifiableSet(Set<? extends T> s) 傳回指定集合的不可修改視圖。

static <K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m) 傳回指定地圖的不可修改視圖。

static <K,V> NavigableMap<K,V> unmodifiableNavigableMap(NavigableMap<K,? extends V> m) 傳回指定可導航地圖的不可修改視圖。

static <T> NavigableSet<T> unmodifiableNavigableSet(NavigableSet<T> s) 傳回指定的可導航集合的不可修改的視圖。

static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K,? extends V> m) 傳回指定排序映射的不可修改視圖。

static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) 傳回指定排序集的不可修改視圖。

更改不可變集合,會出不支援操作異常