Java中的Collections类(工具类)常用方法总结

239 阅读1分钟

Collections类

Collections类是Java中针对集合类的一个工具类,其中提供一系列静态方法。

1. sort(Comparator<? super E>):void List

对集合中的元素排序:

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(2);
        list.add(5);
        list.add(3);
        list.add(4);
        list.add(1);
        Collections.sort(list);
        System.out.println(list);
    }
}
Output:
[1, 2, 3, 4, 5]

2.reverse(List<?>):void

反转集合中的元素:

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        for(int i = 0;i< 5; i++){
            list.add(i+1);
        }
        System.out.println(list);
        Collections.reverse(list);
        System.out.println(list);
    }
}
Output:
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]

3.shuffle(List<?>):void

打乱元素中的元素:

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        for(int i = 0;i< 5; i++){
            list.add(i+1);
        }
        Collections.shuffle(list);
        System.out.println(list);
    }
}
Output:(每次都不同)
eg:[4, 2, 1, 3, 5]

4.fill(List<? super T>,T):void

用T元素替换掉集合中的所有的元素:

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        for(int i = 0;i< 5; i++){
            list.add(i+1);
        }
        System.out.println(list);
        Collections.fill(list,6);
        System.out.println(list);
    }
}
Output:
[1, 2, 3, 4, 5]
[6, 6, 6, 6, 6]

5.copy(List<? super T>,List<? extend T>):void

复制并覆盖相应索引的元素:

public class Main {
    public static void main(String[] args) {
        List<Integer> list1 = new ArrayList<Integer>();
        for(int i = 0;i< 5; i++){
            list1.add(i+1);
        }
        List<Integer> list2 = new ArrayList<Integer>();
        for(int i = 0;i<5;i++){
            list2.add(i+6);
        }
        System.out.println(list1);
        Collections.copy(list1,list2);
        System.out.println(list1);
    }
}
Output:
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]

6.min/max(Collection<? extends T>):T

找到集合中最大/小的元素:

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        for(int i = 0;i< 10; i++){
            list.add(i+1);
        }
        System.out.println(Collections.max(list));
        System.out.println(Collections.min(list));
    }
}
Output:
10
1

7.swap(List<?>,int,int):void

交换集合中指定元素索引的位置:

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        for(int i = 0;i< 10; i++){
            list.add(i+1);
        }
        Collections.swap(list,3,4);
        System.out.println(list);
    }
}
Output:
[1, 2, 3, 5, 4, 6, 7, 8, 9, 10]

8.rotate(List<?>,int):void

集合中的元素向后移m个位置,在后面被遮盖的元素循环到前面来.
(负数向左移动,正数向右移动)

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        for(int i = 0;i< 5; i++){
            list.add(i+1);
        }
        Collections.rotate(list,2);
        System.out.println(list);
        Collections.rotate(list,-2);
        System.out.println(list);
    }
}
Output:
[4, 5, 1, 2, 3]
[1, 2, 3, 4, 5]

9.indexOfSubList(List,List):int / lastIndexOfSubList(List,List):int

找出参数2在参数1第一次出现的位置:

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>(Arrays.asList(1,2,3,3,3,4,5));
        List<Integer> subList = new ArrayList<Integer>();
        subList.add(3);
        System.out.println(Collections.indexOfSubList(list,subList));
        System.out.println(Collections.lastIndexOfSubList(list,subList));
    }
}
Output:
2
4

10.replaceAll(List,T,T):boolean

替换成指定的元素:

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6));
        System.out.println(Collections.replaceAll(list,3,36));
        System.out.println(list);
    }
}
Output:
true
[1, 2, 36, 4, 5, 6]