java提供了一個操作Set、List和Map等集合的工具類:Collections,該工具類提供了大量方法對集合進行排序、查詢和修改等操作,還提供了將集合對象置為不可變、對集合對象實現同步控制等方法。
1.排序操作
方法:
static voidreverse(
List<?>list): 反轉列表中元素的順序。
static voidshuffle(
List<?>list)
: 對List集合元素進行隨機排序。
static voidsort(
List<T>list)
:根據元素的自然順序 對指定列表按升序進行排序static <T> voidsort(
List<T>list,
Comparator<? super T>c)
:根據指定比較器產生的順序對指定列表進行排序。static voidswap(
List<?>list, inti, intj)
:在指定List的指定位置i,j處交換元素。
static voidrotate(
List<?>list, intdistance)
:當distance為正數時,將List集合的后distance個元素“整體”移到前面;當distance為負數時,將list集合的前distance個元素“整體”移到后邊。該方法不會改變集合的長度。
示例:
import java.util.*;/** * Description: * <br/>Copyright (C), 2005-2008, Yeeku.H.Lee * <br/>This PRogram is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */public class TestSort{public static void main(String[] args) {ArrayList nums = new ArrayList();nums.add(2);nums.add(-5);nums.add(3);nums.add(0);//輸出:[2, -5, 3, 0]System.out.println(nums);//將List集合元素的次序反轉Collections.reverse(nums);//輸出:[0, 3, -5, 2]System.out.println(nums);//將List集合元素的按自然順序排序Collections.sort(nums);//輸出:[-5, 0, 2, 3]System.out.println(nums);//將List集合元素的按隨機順序排序Collections.shuffle(nums);//每次輸出的次序不固定System.out.println(nums);//后兩個整體移動到前邊Collections.rotate(nums,2); System.out.println(nums);}}
輸出結果:
[2, -5, 3, 0][0, 3, -5, 2][-5, 0, 2, 3][2, 3, -5, 0][-5, 0, 2, 3]
2.查找、替換操作:
static <T> intbinarySearch(
List<? extends
Comparable<? super T>>list, Tkey):
使用二分搜索法搜索指定列表,以獲得指定對象在List集合中的索引。
**此前必須保證List集合中的元素已經處于有序狀態。
static Object max(Collection coll):根據元素的自然順序,返回給定collection 的最大元素。
static Object max(Collection coll,Comparator comp):根據指定比較器產生的順序,返回給定 collection 的最大元素。
static Object min(Collection coll):根據元素的自然順序,返回給定collection 的最小元素。
static Object min(Collection coll,Comparator comp):根據指定比較器產生的順序,返回給定 collection 的最小元素。
static <T> voidfill(
List<? super T>list, Tobj)
:使用指定元素替換指定列表中的所有元素。static intfrequency(
Collection<?>c,
Objecto)
:返回指定 collection 中等于指定對象的出現次數。static int indexOfSubList(
List<?>source,
List<?>target)
:返回指定源列表中第一次出現指定目標列表的起始位置;如果沒有出現這樣的列表,則返回 -1。static intlastIndexOfSubList(
List<?>source,
List<?>target)
:返回指定源列表中最后一次出現指定目標列表的起始位置;如果沒有出現這樣的列表,則返回 -1。static <T> booleanreplaceAll(
List<T>list, ToldVal, TnewVal)
:使用一個新值替換List對象的所有舊值oldVal。示例:
import java.util.*;public class TestSearch{public static void main(String[] args) {ArrayList nums = new ArrayList();nums.add(2);nums.add(-5);nums.add(3);nums.add(0);//輸出:[2, -5, 3, 0]System.out.println(nums);//輸出最大元素,將輸出3System.out.println(Collections.max(nums));//輸出最小元素,將輸出-5System.out.println(Collections.min(nums));//將nums中的0使用1來代替Collections.replaceAll(nums , 0 , 1);//輸出:[2, -5, 3, 1]System.out.println(nums);//判斷-5 在List集合中出現的次數,返回1System.out.println(Collections.frequency(nums , -5));//對nums集合排序Collections.sort(nums);//輸出:[-5, 1, 2, 3]System.out.println(nums);//只有排序后的List集合才可用二分法查詢,輸出3System.out.println(Collections.binarySearch(nums , 3));}}
輸出結果:
[2, -5, 3, 0]3-5[2, -5, 3, 1]1[-5, 1, 2, 3]3
3.同步控制:
Collectons提供了多個synchronizedXxx()方法·,該方法可以將指定集合包裝成線程同步的集合,從而解決多線程并發訪問集合時的線程安全問題。
正如前面介紹的HashSet,TreeSet,arrayList,LinkedList,HashMap,TreeMap都是線程不安全的。Collections提供了多個靜態方法可以把他們包裝成線程同步的集合。
方法如下:
static <T> Collection<T>synchronizedCollection(
Collection<T>c)
:返回指定 collection 支持的同步(線程安全的)collection。static<T> List<T> synchronizedList(
List<T>list)
:返回指定列表支持的同步(線程安全的)列表。static <K,V> Map<K,V>synchronizedMap(
Map<K,V>m)
:返回由指定映射支持的同步(線程安全的)映射。static <T> Set<T> synchronizedSet(
Set<T>s)
:返回指定 set 支持的同步(線程安全的)set。
。。。等等有好多
示例:
import java.util.*;public class TestSynchronized{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()); }}
多個線程訪問同一個集合時設置。。
4.Collections還可以設置不可變集合,提供了如下三類方法:
emptyXxx(): 返回一個空的、不可變的集合對象,此處的集合既可以是List,也可以是Set,還可以是Map。
singletonXxx(): 返回一個只包含指定對象(只有一個或一個元素)的不可變的集合對象,此處的集合可以是:List,Set,Map。
unmodifiableXxx(): 返回指定集合對象的不可變視圖,此處的集合可以是:List,Set,Map。
上面三類方法的參數是原有的集合對象,返回值是該集合的”只讀“版本。
示例:
import java.util.*;public class TestUnmodifiable{public static void main(String[] args){//創建一個空的、不可改變的List對象List<String> unmodifiableList = Collections.emptyList();//unmodifiableList.add("java"); //添加出現異常:java.lang.UnsupportedOperationExceptionSystem.out.println(unmodifiableList);// []//創建一個只有一個元素,且不可改變的Set對象Set unmodifiableSet = Collections.singleton("Struts2權威指南");//[Struts2權威指南]System.out.println(unmodifiableSet);//創建一個普通Map對象Map scores = new HashMap();scores.put("語文" , 80);scores.put("Java" , 82);//返回普通Map對象對應的不可變版本Map unmodifiableMap = Collections.unmodifiableMap(scores);//下面任意一行代碼都將引發UnsupportedOperationException異常unmodifiableList.add("測試元素");unmodifiableSet.add("測試元素");unmodifiableMap.put("語文",90);}}
用Collections工具類操作集合還是很方便的,省了很多事。。。
轉發請注明出處:http://www.49028c.com/jycboy/p/collections.html
新聞熱點
疑難解答