日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java中Collections常用方法总结(包括sort,copy,reverse等)

發(fā)布時間:2025/3/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中Collections常用方法总结(包括sort,copy,reverse等) 小編覺得挺不錯的,現(xià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);}}運行結(jié)果為:[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);}}
運行結(jié)果為: [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);}}運行結(jié)果為:[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);}}運行結(jié)果為:[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);}}運行結(jié)果為:[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)); } 運行結(jié)果:[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)); } 運行結(jié)果:[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)); } 運行結(jié)果: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)); } 運行結(jié)果: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); } 運行結(jié)果:[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); } 運行結(jié)果:[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);}}運行結(jié)果為:[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);}}運行結(jié)果為:[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);}}運行結(jié)果為:[one, two, three, four, five, six, siven]true[one, two, three, four, five, six, siven eight]

總結(jié)

1. 排序操作(主要針對List接口相關(guān))

reverse(List list):反轉(zhuǎn)指定List集合中元素的順序 shuffle(List list):對List中的元素進行隨機排序(洗牌) sort(List list):對List里的元素根據(jù)自然升序排序 sort(List list, Comparator c):自定義比較器進行排序 swap(List list, int i, int j):將指定List集合中i處元素和j出元素進行交換 rotate(List list, int distance):將所有元素向右移位指定長度,如果distance等于size那么結(jié)果不變 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接口相關(guān))

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的對象沒有排序的話,搜索結(jié)果是不確定的System.out.println("binarySearch在sort之前:" + Collections.binarySearch(list, "c趙五"));Collections.sort(list);// sort之后,結(jié)果出來了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 blockwhile (i.hasNext())foo(i.next());}

4. 設(shè)置不可變集合

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 [我也好, 大家好, 你好]

總結(jié)

以上是生活随笔為你收集整理的java中Collections常用方法总结(包括sort,copy,reverse等)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。