国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

操作集合的工具類:Collections

2019-11-15 01:15:02
字體:
供稿:網(wǎng)友
操作集合的工具類:Collections

java提供了一個操作Set、List和Map等集合的工具類:Collections,該工具類提供了大量方法對集合進(jìn)行排序、查詢和修改等操作,還提供了將集合對象置為不可變、對集合對象實(shí)現(xiàn)同步控制等方法。

1.排序操作

方法:

static voidreverse(List<?>list): 反轉(zhuǎn)列表中元素的順序。

static voidshuffle(List<?>list): 對List集合元素進(jìn)行隨機(jī)排序。

static voidsort(List<T>list):根據(jù)元素的自然順序 對指定列表按升序進(jìn)行排序static <T> voidsort(List<T>list, Comparator<? super T>c):根據(jù)指定比較器產(chǎn)生的順序?qū)χ付斜磉M(jìn)行排序。static voidswap(List<?>list, inti, intj):在指定List的指定位置i,j處交換元素。

static voidrotate(List<?>list, intdistance):當(dāng)distance為正數(shù)時,將List集合的后distance個元素“整體”移到前面;當(dāng)distance為負(fù)數(shù)時,將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集合元素的次序反轉(zhuǎn)Collections.reverse(nums);//輸出:[0, 3, -5, 2]System.out.println(nums);//將List集合元素的按自然順序排序Collections.sort(nums);//輸出:[-5, 0, 2, 3]System.out.println(nums);//將List集合元素的按隨機(jī)順序排序Collections.shuffle(nums);//每次輸出的次序不固定System.out.println(nums);//后兩個整體移動到前邊Collections.rotate(nums,2);        System.out.println(nums);}}

  輸出結(jié)果:

[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集合中的元素已經(jīng)處于有序狀態(tài)。

static Object max(Collection coll):根據(jù)元素的自然順序,返回給定collection 的最大元素。

static Object max(Collection coll,Comparator comp):根據(jù)指定比較器產(chǎn)生的順序,返回給定 collection 的最大元素。

static Object min(Collection coll):根據(jù)元素的自然順序,返回給定collection 的最小元素。

static Object min(Collection coll,Comparator comp):根據(jù)指定比較器產(chǎn)生的順序,返回給定 collection 的最小元素。

static <T> voidfill(List<? super T>list, Tobj):使用指定元素替換指定列表中的所有元素。static intfrequency(Collection<?>c, Objecto):返回指定 collection 中等于指定對象的出現(xiàn)次數(shù)。static int indexOfSubList(List<?>source, List<?>target):返回指定源列表中第一次出現(xiàn)指定目標(biāo)列表的起始位置;如果沒有出現(xiàn)這樣的列表,則返回 -1。static intlastIndexOfSubList(List<?>source, List<?>target):返回指定源列表中最后一次出現(xiàn)指定目標(biāo)列表的起始位置;如果沒有出現(xiàn)這樣的列表,則返回 -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集合中出現(xiàn)的次數(shù),返回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));}}

  輸出結(jié)果:

[2, -5, 3, 0]3-5[2, -5, 3, 1]1[-5, 1, 2, 3]3

3.同步控制:

Collectons提供了多個synchronizedXxx()方法·,該方法可以將指定集合包裝成線程同步的集合,從而解決多線程并發(fā)訪問集合時的線程安全問題。

正如前面介紹的HashSet,TreeSet,arrayList,LinkedList,HashMap,TreeMap都是線程不安全的。Collections提供了多個靜態(tài)方法可以把他們包裝成線程同步的集合。

方法如下:

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){//下面程序創(chuàng)建了四個同步的集合對象Collection c = Collections.synchronizedCollection(new ArrayList());List list = Collections.synchronizedList(new ArrayList()); Set s = Collections.synchronizedSet(new HashSet()); Map m = Collections.synchronizedMap(new HashMap()); }}

  多個線程訪問同一個集合時設(shè)置。。

4.Collections還可以設(shè)置不可變集合,提供了如下三類方法:

emptyXxx(): 返回一個空的、不可變的集合對象,此處的集合既可以是List,也可以是Set,還可以是Map。

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

unmodifiableXxx(): 返回指定集合對象的不可變視圖,此處的集合可以是:List,Set,Map。

上面三類方法的參數(shù)是原有的集合對象,返回值是該集合的”只讀“版本。

示例:

import java.util.*;public class TestUnmodifiable{public static void main(String[] args){//創(chuàng)建一個空的、不可改變的List對象List<String> unmodifiableList = Collections.emptyList();//unmodifiableList.add("java");  //添加出現(xiàn)異常:java.lang.UnsupportedOperationExceptionSystem.out.println(unmodifiableList);// []//創(chuàng)建一個只有一個元素,且不可改變的Set對象Set unmodifiableSet = Collections.singleton("Struts2權(quán)威指南");//[Struts2權(quán)威指南]System.out.println(unmodifiableSet);//創(chuàng)建一個普通Map對象Map scores = new HashMap();scores.put("語文" , 80);scores.put("Java" , 82);//返回普通Map對象對應(yīng)的不可變版本Map unmodifiableMap = Collections.unmodifiableMap(scores);//下面任意一行代碼都將引發(fā)UnsupportedOperationException異常unmodifiableList.add("測試元素");unmodifiableSet.add("測試元素");unmodifiableMap.put("語文",90);}}

  

用Collections工具類操作集合還是很方便的,省了很多事。。。

轉(zhuǎn)發(fā)請注明出處:http://www.survivalescaperooms.com/jycboy/p/collections.html


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄浦区| 全南县| 安泽县| 临漳县| 房产| 资兴市| 泰来县| 竹北市| 青海省| 马龙县| 泾川县| 泉州市| 岳池县| 葫芦岛市| 海宁市| 乌什县| 启东市| 洪雅县| 砚山县| 江津市| 涿鹿县| 桦川县| 青海省| 平阳县| 晋州市| 湖北省| 鄂托克旗| 满洲里市| 桓仁| 方山县| 博客| 高州市| 安陆市| 雷山县| 集贤县| 绥德县| 永安市| 波密县| 樟树市| 福海县| 万山特区|