關于java中Collections類方法-創(chuàng)新互聯(lián)

1、sort(Collection)方法的使用(含義:對集合進行排序)。
例:對已知集合c進行排序
public class Practice {
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
Collections.sort(c);
System.out.println(c);
}
}
運行結果為:[l, o, v, e]
[e, l, o, v]
2.reverse()方法的使用(含義:反轉(zhuǎn)集合中元素的順序)。
public class Practice {
public static void main(String[] args){
List list = Arrays.asList("one two three four five six siven".split(""));//無空格
System.out.println(list);
List list = Arrays.asList("one two three four five six siven".split(" "));//兩個空格
System.out.println(list);
List list = Arrays.asList("one two three four five six siven".split(" "));//一個空格
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
}
}
運行結果為:
[o, n, e, , t, w, o, , t, h, r, e, e, , f, o, u, r, , f, i, v, e, , s, i, x, , s, i, v, e, n]
[one two three four five six siven]
[one, two, three, four, five, six, siven]
[siven, six, five, four, three, two, one]
3.shuffle(Collection)方法的使用(含義:對集合進行隨機排序)。
shuffle(Collection)的簡單示例
public class Practice {
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
Collections.shuffle(c);
System.out.println(c);
Collections.shuffle(c);
System.out.println(c);
}
}
運行結果為:[l, o, v, e]
[l, v, e, o]
[o, v, e, l]
4.fill(List list,Object o)方法的使用(含義:用對象o替換集合list中的所有元素)
public class Practice {
public static void main(String[] args){
List m = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(m);
Collections.fill(m, "青鳥52T25小龍");
System.out.println(m);
}
}
運行結果為:
[one, two, three, four, five, six, siven]
[青鳥52T25小龍, 青鳥52T25小龍, 青鳥52T25小龍, 青鳥52T25小龍, 青鳥52T25小龍, 青鳥52T25小龍, 青鳥52T25小龍]
5.copy(List m,List n)方法的使用(含義:將集合n中的元素全部復制到m中,并且覆蓋相應索引的元素)。
public class Practice {
public static void main(String[] args){
List m = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(m);
List n = Arrays.asList("我 是 復制過來的哈".split(" "));
System.out.println(n);
Collections.copy(m,n);
System.out.println(m);
}
}
運行結果為:[one, two, three, four, five, six, siven]
[我, 是, 復制過來的哈]
[我, 是, 復制過來的哈, four, five, six, siven]
6.min(Collection),min(Collection,Comparator)方法的使用(前者采用Collection內(nèi)含自然比較法,后者采用Comparator進行比較)。
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
System.out.println(Collections.min(c));
}
運行結果:[l, o, v, e]
e
7.max(Collection),max(Collection,Comparator)方法的使用(前者采用Collection內(nèi)含自然比較法,后者采用Comparator進行比較)。
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
System.out.println(Collections.max(c));
}
運行結果:[l, o, v, e]
v
8.indexOfSubList(List list,List subList)方法的使用(含義:查找subList在list中首次出現(xiàn)位置的索引)。
public static void main(String[] args){
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 6, 6, 7, 3));
ArrayList<Integer> targetList = new ArrayList<>(Arrays.asList(6));
System.out.println(Collections.indexOfSubList(intList, targetList));
}
運行結果:5
9.lastIndexOfSubList(List source,List target)方法的使用與上例方法的使用相同
public static void main(String[] args){
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 6, 6, 7, 3));
ArrayList<Integer> targetList = new ArrayList<>(Arrays.asList(6));
System.out.println(Collections.lastIndexOfSubList(intList, targetList));
}
運行結果:7
10.rotate(List list,int m)方法的使用(含義:集合中的元素向后移m個位置,在后面被遮蓋的元素循環(huán)到前面來)。移動列表中的元素,負數(shù)向左移動,正數(shù)向右移動
public static void main(String[] args){
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(intList);
Collections.rotate(intList, 1);
System.out.println(intList);
}
運行結果:[1, 2, 3, 4, 5]
[5, 1, 2, 3, 4]
public static void main(String[] args){
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(intList);
Collections.rotate(intList, -1);
System.out.println(intList);
}
運行結果:[1, 2, 3, 4, 5]
[2, 3, 4, 5, 1]
11.swap(List list,int i,int j)方法的使用(含義:交換集合中指定元素索引的位置)
public class Practice {
public static void main(String[] args){
List m = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(m);
Collections.swap(m, 2, 3);
System.out.println(m);
}
}
運行結果為:
[one, two, three, four, five, six, siven]
[one, two, four, three, five, six, siven]
12.binarySearch(Collection,Object)方法的使用(含義:查找指定集合中的元素,返回所查找元素的索引)。
binarySearch(Collection,Object)的簡單示例
public class Practice {
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
int m = Collections.binarySearch(c, "o");
System.out.println(m);
}
}
運行結果為:[l, o, v, e]
1
13.replaceAll(List list,Object old,Object new)方法的使用(含義:替換批定元素為某元素,若要替換的值存在剛返回true,反之返回false)。
public class Practice {
public static void main(String[] args){
List list = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(list);
List subList = Arrays.asList("three four five six".split(" "));
System.out.println(Collections.replaceAll(list, "siven", "siven eight"));
System.out.println(list);
}
}
運行結果為:
[one, two, three, four, five, six, siven]
true
[one, two, three, four, five, six, siven eight]
總結

成都創(chuàng)新互聯(lián)公司主營鐵鋒網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,app軟件開發(fā),鐵鋒h5成都小程序開發(fā)搭建,鐵鋒網(wǎng)站營銷推廣歡迎鐵鋒等地區(qū)企業(yè)咨詢
  1. 排序操作(主要針對List接口相關)
    reverse(List list):反轉(zhuǎn)指定List集合中元素的順序
    shuffle(List list):對List中的元素進行隨機排序(洗牌)
    sort(List list):對List里的元素根據(jù)AxiTrader返傭www.kaifx.cn/broker/axitrader.html自然升序排序
    sort(List list, Comparator c):自定義比較器進行排序
    swap(List list, int i, int j):將指定List集合中i處元素和j出元素進行交換
    rotate(List list, int distance):將所有元素向右移位指定長度,如果distance等于size那么結果不變
    public void testSort() {
    System.out.println("原始順序:" + list);
    Collections.reverse(list);
    System.out.println("reverse后順序:" + list);
    Collections.shuffle(list);
    System.out.println("shuffle后順序:" + list);
    Collections.swap(list, 1, 3);
    System.out.println("swap后順序:" + list);
    Collections.sort(list);
    System.out.println("sort后順序:" + list);
    Collections.rotate(list, 1);
    System.out.println("rotate后順序:" + list);
    }
    輸出
    原始順序:[b張三, d孫六, a李四, e錢七, c趙五]
    reverse后順序:[c趙五, e錢七, a李四, d孫六, b張三]
    shuffle后順序:[b張三, c趙五, d孫六, e錢七, a李四]
    swap后順序:[b張三, e錢七, d孫六, c趙五, a李四]
    sort后順序:[a李四, b張三, c趙五, d孫六, e錢七]
    rotate后順序:[e錢七, a李四, b張三, c趙五, d孫六]
  2. 查找和替換(主要針對Collection接口相關)
    binarySearch(List list, Object key):使用二分搜索法,以獲得指定對象在List中的索引,前提是集合已經(jīng)排序
    max(Collection coll):返回大元素
    max(Collection coll, Comparator comp):根據(jù)自定義比較器,返回大元素
    min(Collection coll):返回最小元素
    min(Collection coll, Comparator comp):根據(jù)自定義比較器,返回最小元素
    fill(List list, Object obj):使用指定對象填充
    frequency(Collection Object o):返回指定集合中指定對象出現(xiàn)的次數(shù)
    replaceAll(List list, Object old, Object new):替換
    public void testSearch() {
    System.out.println("給定的list:" + list);
    System.out.println("max:" + Collections.max(list));
    System.out.println("min:" + Collections.min(list));
    System.out.println("frequency:" + Collections.frequency(list, "a李四"));
    Collections.replaceAll(list, "a李四", "aa李四");
    System.out.println("replaceAll之后:" + list);
    // 如果binarySearch的對象沒有排序的話,搜索結果是不確定的
    System.out.println("binarySearch在sort之前:" + Collections.binarySearch(list, "c趙五"));
    Collections.sort(list);
    // sort之后,結果出來了
    System.out.println("binarySearch在sort之后:" + Collections.binarySearch(list, "c趙五"));
    Collections.fill(list, "A");
    System.out.println("fill:" + list);
    }
    輸出
    給定的list:[b張三, d孫六, a李四, e錢七, c趙五]
    max:e錢七
    min:a李四
    frequency:1
    replaceAll之后:[b張三, d孫六, aa李四, e錢七, c趙五]
    binarySearch在sort之前:-4
    binarySearch在sort之后:2
    fill:[A, A, A, A, A]
  3. 同步控制
    Collections工具類中提供了多個synchronizedXxx方法,該方法返回指定集合對象對應的同步對象,從而解決多線程并發(fā)訪問集合時線程的安全問題。HashSet、ArrayList、HashMap都是線程不安全的,如果需要考慮同步,則使用這些方法。這些方法主要有:synchronizedSet、synchronizedSortedSet、synchronizedList、synchronizedMap、synchronizedSortedMap。
    特別需要指出的是,在使用迭代方法遍歷集合時需要手工同步返回的集合。
    Map m = Collections.synchronizedMap(new HashMap());
    ...
    Set s = m.keySet(); // Needn't be in synchronized block
    ...
    synchronized (m) { // Synchronizing on m, not s!
    Iterator i = s.iterator(); // Must be in synchronized block
    while (i.hasNext())
    foo(i.next());
    }
  4. 設置不可變集合
    Collections有三類方法可返回一個不可變集合:
    emptyXxx():返回一個空的不可變的集合對象
    singletonXxx():返回一個只包含指定對象的,不可變的集合對象。
    unmodifiableXxx():返回指定集合對象的不可變視圖
    public void testUnmodifiable() {
    System.out.println("給定的list:" + list);
    List<String> unmodList = Collections.unmodifiableList(list);
    unmodList.add("再加個試試!"); // 拋出:java.lang.UnsupportedOperationException
    // 這一行不會執(zhí)行了
    System.out.println("新的unmodList:" + unmodList);
    }
  5. 其它
    disjoint(Collection<?> c1, Collection<?> c2) - 如果兩個指定 collection 中沒有相同的元素,則返回 true。
    addAll(Collection<? super T> c, T... a) - 一種方便的方式,將所有指定元素添加到指定 collection 中。示范:
    Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon");
    Comparator<T> reverseOrder(Comparator<T> cmp) - 返回一個比較器,它強行反轉(zhuǎn)指定比較器的順序。如果指定比較器為 null,則此方法等同于 reverseOrder()(換句話說,它返回一個比較器,該比較器將強行反轉(zhuǎn)實現(xiàn) Comparable 接口那些對象 collection 上的自然順序)。
    public void testOther() {
    List<String> list1 = new ArrayList<String>();
    List<String> list2 = new ArrayList<String>();
    // addAll增加變長參數(shù)
    Collections.addAll(list1, "大家好", "你好","我也好");
    Collections.addAll(list2, "大家好", "a李四","我也好");
    // disjoint檢查兩個Collection是否的交集
    boolean b1 = Collections.disjoint(list, list1);
    boolean b2 = Collections.disjoint(list, list2);
    System.out.println(b1 + "\t" + b2);
    // 利用reverseOrder倒序
    Collections.sort(list1, Collections.reverseOrder());
    System.out.println(list1);
    }
    輸出
    true false
    [我也好, 大家好, 你好]

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

網(wǎng)站名稱:關于java中Collections類方法-創(chuàng)新互聯(lián)
URL地址:http://muchs.cn/article42/cdogec.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化、小程序開發(fā)網(wǎng)站營銷網(wǎng)站設計公司、品牌網(wǎng)站建設域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作