Java 集合 Collections工具類

Java提供了一個操做Set、List和Map等集合的工具類:Collections,該工具類提供了大量方法對集合進行排序、查詢和修改等操做,還提供了將集合對象置爲不可變、對集合對象實現同步控制等方法安全

排序操做

void reverse(List list):反轉指定List集合中元素的順序多線程

  • void shuffle(List list):對List集合元素進行隨機排序(shuffle方法模擬了「洗牌」動做)併發

  • void sort(List list):根據元素的天然順序對指定List集合的元素按升序進行排序框架

  • void sort(List list, Comparator c):根據指定Comparator比較器產生的順序對List集合元素進行排序工具

  • void swap(List list, int i, int j):在指定List集合中的i處元素和j處元素進行交換線程

  • void rotate(List list, int distance):當distance爲正數時,將List集合的後distance個元素「總體」移到前面;當distance爲負數時,將list集合的前distance個元素「總體」移到後邊。該方法不會改變集合的長度code

public class SortTest
{
    public static void main(String[] args)
    {
        ArrayList nums = new ArrayList();
        nums.add(2);
        nums.add(-5);
        nums.add(3);
        nums.add(0);
        System.out.println(nums); // 輸出:[2, -5, 3, 0]
        Collections.reverse(nums); // 將List集合元素的次序反轉
        System.out.println(nums); // 輸出:[0, 3, -5, 2]
        Collections.sort(nums); // 將List集合元素的按天然順序排序
        System.out.println(nums); // 輸出:[-5, 0, 2, 3]
        Collections.shuffle(nums); // 將List集合元素的按隨機順序排序
        System.out.println(nums); // 每次輸出的次序不固定
    }
}

查找、替換操做

  • int binarySearch(List list, Object key):使用二分搜索法搜索指定列表,以得到指定對象在List集合中的索引。
    此前必須保證List集合中的元素已經處於有序狀態對象

  • Object max(Collection coll):根據元素的天然順序,返回給定collection中的最大元素排序

  • Object max(Collection coll, Comparator comp):根據指定Comparator比較器產生的順序,返回給定collection中的最大元素索引

  • Object min(Collection coll):根據元素的天然順序,返回給定collection中的最小元素

  • Object min(Collection coll, Comparator comp):根據指定Comparator比較器產生的順序,返回給定 collection中的最小元素

  • void fill(List list, Object obj):使用指定元素obj替換指定List集合中的全部元素

  • int frequency(Collection c, Object o):返回指定collection中指定元素的出現次數

  • int indexOfSubList(List source, List target):返回子List對象在父List對象中第一次出現的位置索引;若是沒有出現這樣的列表,則返回-1

  • int lastIndexOfSubList(List source, List target):返回子List對象在父List對象中最後一次出現的位置索引;若是沒有出現這樣的列表,則返回-1

  • boolean replaceAll(List list, Object oldVal, Object newVal):使用一個新值newVal替換List對象的全部舊值oldVal

public class SearchTest
{
    public static void main(String[] args)
    {
        ArrayList nums = new ArrayList();
        nums.add(23);
        nums.add(0);
        nums.add(9);
        nums.add(3);
        System.out.println(nums); // 輸出:[23, 0, 9, 3]
        System.out.println(Collections.max(nums)); // 輸出最大元素,將輸出23
        System.out.println(Collections.min(nums)); // 輸出最小元素,將輸出0
        Collections.replaceAll(nums , 0 , 1); // 將nums中的0使用1來代替
        System.out.println(nums); // 輸出:[23, 1, 9, 3]
        // 判斷-5在List集合中出現的次數,返回1
        System.out.println(Collections.frequency(nums , 23));
        Collections.sort(nums); // 對nums集合排序
        System.out.println(nums); // 輸出:[1, 3, 9, 23]
        //只有排序後的List集合纔可用二分法查詢,輸出3
        System.out.println(Collections.binarySearch(nums , 23));
    }
}

同步控制

Collectons提供了多個synchronizedXxx()方法,該方法能夠將指定集合包裝成線程同步的集合,從而解決多線程併發訪問集合時的線程安全問題

Java經常使用的集合框架中的實現類HashSet、TreeSet、ArrayList、LinkedList、HashMap、TreeMap都是線程不安全的。Collections提供了多個靜態方法能夠把他們包裝成線程同步的集合

  • Collection synchronizedCollection(Collection c):返回指定collection支持的同步(線程安全的)collection

  • List synchronizedList(List list):返回指定List支持的同步(線程安全的)List

  • Map synchronizedMap(Map m):返回由指定Map支持的同步(線程安全的)Map

  • Set synchronizedSet(Set s):返回指定Set支持的同步(線程安全的)Set

public class SynchronizedTest
{
    public static void main(String[] args)
    {
        // 下面程序建立了四個線程安全的集合對象
        Collection c = Collections
            .synchronizedCollection(new ArrayList());
        List list = Collections.synchronizedList(new ArrayList());
        Set s = Collections.synchronizedSet(new HashSet());
        Map m = Collections.synchronizedMap(new HashMap());
    }
}

設置不可變集合

Collections提供了以下三個方法來返回一個不可變集合:

emptyXxx():返回一個空的、不可變的集合對象,此處的集合既能夠是List、SortedSet、Set、SortedMap、Map

singletonXxx():返回一個只包含指定對象(只有一個或一個元素)的、不可變的集合對象,此處的集合能夠是List和Map

unmodifiableXxx():返回指定集合對象的不可變視圖,此處的集合能夠是:List、SortedSet、Set、SortedMap、Map

經過上面Collections提供的三類方法,能夠生成「只讀」的Collection或Map

public class UnmodifiableTest
{
    public static void main(String[] args)
    {
        // 建立一個空的、不可改變的List對象
        List unmodifiableList = Collections.emptyList();
        // 建立一個只有一個元素,且不可改變的Set對象
        Set unmodifiableSet = Collections.singleton("MVP");
        // 建立一個普通Map對象
        Map player = new HashMap();
        player.put("勒布朗詹姆斯", 23);
        player.put("克萊湯普森", 11);
        // 返回普通Map對象對應的不可變版本
        Map unmodifiableMap = Collections.unmodifiableMap(player);
        // 下面任意一行代碼都將引起UnsupportedOperationException異常
        unmodifiableList.add("德懷恩韋德");   //①
        unmodifiableSet.add("克里斯波什");    //②
        unmodifiableMap.put("語伊戈達拉", 9);   //③
    }
}

不可變的集合對象只能訪問集合元素,不可修改集合元素,上述①、②、③處代碼都將引起UnsupportedOperationException異常

相關文章
相關標籤/搜索