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

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]

11.synchronizedXxx方法

可以将某集合转化成线程安全的容器之后再使用。

eg:
public class Main {
    public static void main(String[] args) {
        List<String> slist = Collections.synchronizedList(new ArrayList<String>());
        ...
    }
}

12.unmodifiableCollection(Collection<? extends T>):Collection

将集合变为不可修改。

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(10);
        Collection<Integer> clist = Collections.unmodifiableCollection(list);
        try{
            clist.add(10);
        }catch (Exception e){
            System.out.println("Exception");
        }
    }
}
Output:
Exception

若有不当之处,欢迎指正! ^ - ^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值