集合工具类Collections的使用

1.sort() —根据其元素的升序将指定列表排序。

官方注释

//根据其元素的升序将指定列表排序。
Sorts the specified list into ascending order, according to the {@linkplain Comparable natural ordering} of its elements.
//列表中的所有元素必须实现该接口。
All elements in the list must implement the {@link Comparable} interface.
//此外,列表中的所有元素都必须相互可比(该列表中任何元素都不能抛出类型转换异常)。
Furthermore, all elements in the list must be mutually comparable (that is, {@code e1.compareTo(e2)} must not throw a {@code ClassCastException} for any elements {@code e1} and {@code e2} in the list).

例1:

        List<Integer> list = new ArrayList<>();
        list.add(6);
        list.add(1);
        list.add(3);
        list.add(2);
		//Collections的排序方法sort()
        Collections.sort(list);
        System.out.println(list);

输出结果

[1, 2, 3, 6]

例2:

        List<String> lists = Arrays.asList("a","c","b");
        System.out.println(lists);
        //匿名内部类
        lists.sort(new Comparator<String>() {
            public int compare(String o1, String o2) {
                //指定比较规则,按照首字母降序来排列
                return o2.charAt(0) - o1.charAt(0);
            }
        });
        //lambda表达式
/*        lists.sort((o1, o2) -> {
            //指定比较规则,按照首字母降序来排列
            return o2.charAt(o2.length() - 1) - o1.charAt(o1.length() - 1);
        });*/
        System.out.println(lists);

输出结果

[a, c, b]
[c, b, a]
2.addAll() —将所有指定的元素添加到指定的集合中。

官方注释

//将所有指定的元素添加到指定的集合中。
Adds all of the specified elements to the specified collection.
//要添加的元素可以单独指定或作为数组指定。
Elements to be added may be specified individually or as an array.
//此便捷方法与Arrays.asList()方法类似,但是这种方法在大多数情况下运行速度更快。
The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under most implementations.
//当分别指定元素时,此方法提供了一种向现有集合中添加一些元素的便捷方法:
When elements are specified individually, this method provides a convenient way to add a few elements to an existing collection:
//举例 Collections.addAll(集合/对象, 字符串, 字符串);
Collections.addAll(flavors, “Peaches 'n Plutonium”, “Rocky Racoon”);

举例

		List<Integer> list = new ArrayList<>();

        list.add(6);
        list.add(1);
        list.add(3);
        list.add(2);
		//Collections的添加方法addAll()
        Collections.addAll(list, 4 ,5);
        System.out.println(list);

输出结果

[6, 1, 3, 2, 4, 5]
3.reverse() —将所有指定的元素添加到指定的集合中。

官方注释

//反转指定列表中元素的顺序
Reverses the order of the elements in the specified list.
//此方法线性运行。
This method runs in linear time.

举例

		List<Integer> list = new ArrayList<>();

        list.add(6);
        list.add(1);
        list.add(3);
        list.add(2);
		//Collections的反转方法reverse()
        Collections.reverse(list);
        System.out.println(list);

输出结果

[2, 3, 1, 6]
4.shuffle() —随机排列指定的列表。(打乱列表元素顺序)

官方注释

//使用默认的随机性源随机排列指定的列表。
Randomly permutes the specified list using a default source of randomness.
//所有排列发生的可能性几乎相等。
All permutations occur with approximately equal likelihood.
//由于默认来源,在前面的描述中使用了“大约”对冲随机性近似仅是独立选择的位的无偏源。
The hedge “approximately” is used in the foregoing description because default source of randomness is only approximately an unbiased source of independently chosen bits.
//如果它是随机选择位的理想来源,那么该算法将选择具有完美一致性的排列。
If it were a perfect source of randomly chosen bits, then the algorithm would choose permutations with perfect uniformity.
//此实现从最后一个元素到第二个元素向后遍历列表,反复将随机选择的元素交换到“当前位置”。
This implementation traverses the list backwards, from the last element up to the second, repeatedly swapping a randomly selected element into the “current position”.
//从运行的列表部分中随机选择元素从第一个元素到当前位置(包括首尾)。
Elements are randomly selected from the portion of the list that runs from the first element to the current position, inclusive.
//此方法线性运行。
This method runs in linear time.
//如果指定的列表未实现RandomAccess接口并且很大,该实现将指定的列表转储到数组中,然后重新组合,并将经过改组的数组转储到列表中。
If the specified list does not implement the {@link RandomAccess} interface and is large, this implementation dumps the specified list into an array before shuffling it, and dumps the shuffled array back into the list.
//这避免了因改组“顺序访问”列表而导致的二次行为。
This avoids the quadratic behavior that would result from shuffling a “sequential access” list in place.

看不懂?太多了?迷迷糊糊?
就记得这个方法是 将列表中的元素打乱随机排序、线性运行。
举例

		List<Integer> list = new ArrayList<>();

        list.add(6);
        list.add(1);
        list.add(3);
        list.add(2);
        System.out.println("重排前" + list);
		//Collections的重排方法shuffle() 每次重排结果不一致
        Collections.shuffle(list);
        System.out.println("重排后" + list);

输出结果

//第一次运行
重排前[6, 1, 3, 2]
重排后[3, 2, 6, 1]
//第二次运行
重排前[6, 1, 3, 2]
重排后[3, 1, 6, 2]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是光吖。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值