Collections类的静态工具方法

Collections

The Java Collections class, java.util.Collections, contains a long list of utility methods for working with collections in Java. In this Collections tutorial I will go through some of the most useful of these methods.

addAll

Java Collections addAll()方法可以向Collection中添加可变数量的元素(通常是List或Set)。以下是调用Collections addAll()方法的Java代码示例:

        List<String> list = new ArrayList<>();
        Collections.addAll(list, "element1", "element2", "element3");

        list.forEach((element) -> System.out.println(element));

binarySearch

搜索List列表方法,搜索列表之前,必须对列表进行升序排序

        List<String> list = new ArrayList<>();
        Collections.addAll(list, "element1", "element2", "element3");

        list.forEach((element) -> System.out.println(element));

        Collections.sort(list);

        int index = Collections.binarySearch(list, "element2");

        System.out.println(index);

copy

Collections copy()方法可以将一个List的所有元素复制到另一个List中。

拷贝一个ArrayList对象到另一个ArrayList对象中,正好发现Collections有一个copy方法。可是不好用啊总是报错。查看api才知道,它的capacity(容纳能力大小)可以指定(最好指定)。而初始化时size的大小永远默认为0,只有在进行add和remove等相关操作 时,size的大小才变化。然而进行copy()时候,首先做的是将desc的size和src的size大小进行比较,只有当desc的 size 大于或者等于src的size时才进行拷贝,否则抛出IndexOutOfBoundsException异常
这段话引用自:https://blog.csdn.net/tiantiandjava/article/details/51072173.

        List<String> source = new ArrayList<>();
        Collections.addAll(source, "element1", "element2", "element3");


        List<String> destination = new ArrayList<>();
        Collections.addAll(destination, "", "", "", "");
//      赋值拷贝元素
        Collections.copy(destination, source);

        destination.forEach((element) -> System.out.println(element));
        System.out.println(destination.size());

reverse

Collections reverse()方法可以反转Java列表中的元素

        List<String> list = new ArrayList<>();
        Collections.addAll(list, "element1", "element2", "element3");

        Collections.reverse(list);

        list.forEach((e) -> System.out.println(e));

shuffle

Collections shuffle()方法可以对List的元素进行随机排序。

        List<String> list = new ArrayList<>();
        Collections.addAll(list, "element1", "element2", "element3", "element4", "element5", "element6", "element7");

        Collections.shuffle(list);

        list.forEach((e) -> System.out.println(e));

sort

Collections sort()方法可以对Java列表进行排序。主要了解他的排序规则

min

Collections min()方法可以根据元素的自然顺序找到List中的最小元素

        List source = new ArrayList();
        source.add("1");
        source.add("2");
        source.add("3");

        String min = (String) Collections.min(source);

        System.out.println(min);

max

Collections max()方法可以根据元素的自然顺序在List中找到最大的元素

replaceAll

Java Collections replaceAll()方法可以将列表中某个或某些符合要求的元素全部替换为另一个元素。将要替换的元素和要替换它的元素作为参数传递给replaceAll()方法。如果替换了任何元素,Collections replaceAll()方法将返回true;如果没有,则返回false。

        List list = new ArrayList();
        list.add("A");
        list.add("B");
        list.add("A");

        boolean replacedAny = Collections.replaceAll(list, "A", "C");

        list.forEach((e) -> System.out.println(e));

unmodifiableSet

Java Collections类中的unmodifiableSet()方法可以从常规Java Set中创建一个不可变(不可修改)的Set

		Set normalSet = new HashSet();
        Set immutableSet = Collections.unmodifiableSet(normalSet);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值