關於java中Collections類方法

一、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()方法的使用(含義:反轉集合中元素的順序)。
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內含天然比較法,後者採用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內含天然比較法,後者採用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中首次出現位置的索引)。
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個位置,在後面被遮蓋的元素循環到前面來)。移動列表中的元素,負數向左移動,正數向右移動
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]
總結html

  1. 排序操做(主要針對List接口相關)
    reverse(List list):反轉指定List集合中元素的順序
    shuffle(List list):對List中的元素進行隨機排序(洗牌)
    sort(List list):對List裏的元素根據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中的索引,前提是集合已經排序
    max(Collection coll):返回最大元素
    max(Collection coll, Comparator comp):根據自定義比較器,返回最大元素
    min(Collection coll):返回最小元素
    min(Collection coll, Comparator comp):根據自定義比較器,返回最小元素
    fill(List list, Object obj):使用指定對象填充
    frequency(Collection Object o):返回指定集合中指定對象出現的次數
    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方法,該方法返回指定集合對象對應的同步對象,從而解決多線程併發訪問集合時線程的安全問題。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
    // 這一行不會執行了
    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) - 返回一個比較器,它強行反轉指定比較器的順序。若是指定比較器爲 null,則此方法等同於 reverseOrder()(換句話說,它返回一個比較器,該比較器將強行反轉實現 Comparable 接口那些對象 collection 上的天然順序)。public void testOther() {List<String> list1 = new ArrayList<String>();List<String> list2 = new ArrayList<String>(); // addAll增長變長參數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[我也好, 你們好, 你好]
相關文章
相關標籤/搜索