Collections类

这篇博客详细介绍了Java.util.Collections类的常用方法,包括获取集合最大值、最小值,复制集合,反转元素,随机置换,排序以及交换元素位置等操作。通过实例展示了如何使用匿名内部类创建比较器进行自定义排序,并给出了每个方法的执行结果。
摘要由CSDN通过智能技术生成

Collections类:java.util.Collections类主要提供了对集合操作或者返回集合的静态方法。

常用方法

方法功能
static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)根据元素的自然顺序返回给定集合的最大元素
static T max(Collection<? extends T> coll, Comparator<?super T> comp)根据指定比较器引发的顺序返回给定集合的最大元素
static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)根据元素的自然顺序返回给定集合的最小元素
static T min(Collection<? extends T> coll, Comparator<?super T> comp)根据指定比较器引发的顺序返回给定集合的最小元素
static void copy(List<? super T> dest, List<? extends T> src)将一个列表中的所有元素复制到另一个列表中
static void reverse(List<?> list)反转指定列表中元素的顺序
static void shuffle(List<?> list)使用默认的随机源随机置换指定的列表
static <T extends Comparable<? super T>> void sort(List list)根据其元素的自然顺序将指定列表按升序排序
static void sort(List list, Comparator<? super T> c)根据指定比较器指定的顺序对指定列表进行排序
static void swap(List<?> list, int i, int j)交换指定列表中指定位置的元素

具体方法如下:


import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Collections类主要提供了对集合操作或者返回集合的静态方法
 */
public class CollectionsTest {

    public static void main(String[] args) {
        // 构建一维数组并初始化
        Integer[] s1 = new Integer[]{10, 50, 42, 30, 21};
        // 将一维数组转换为集合 Arrays.asList()方法
        List<Integer> l1 = Arrays.asList(s1);
        System.out.println(l1);

        // 方法一 :获取集合中的最大值
        System.out.println("集合中的最大值为" + Collections.max(l1));

        // 方法二:通过匿名内部类构建比较器获取最大值
        Comparator<Integer> comparatorMax = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        };
        System.out.println("通过比较器获取的最大值结果为" + Collections.max(l1, comparatorMax));

        // 方法三:获取集合中的最小值
        System.out.println("集合中的最小值为" + Collections.min(l1));

        // 方法四:通过匿名内部类构建比较器获取最小值
        Comparator<Integer> comparatorMin = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        };
        System.out.println("通过比较器获取的最小值结果为" + Collections.min(l1, comparatorMin));

        // 方法五:将集合中的元素复制到另外一个集合中
        List<Integer> l2 = Arrays.asList(new Integer[10]); // 构建一个容量比第一个数组大的一维数组并且转为集合类型
        // 将l1中的元素复制到l2中
        Collections.copy(l2, l1);
        System.out.println("复制后集合中的元素为" + l2);

        // 方法六:将集合进行反转
        Collections.reverse(l1);
        System.out.println("反转后的结果为" + l1);

        // 方法七:使用默认的随机源随机置换指定的列表
        Collections.shuffle(l1);
        System.out.println("随机置换后的结果为" + l1);

        // 方法八:将集合进行升序排序
        Collections.sort(l1);
        System.out.println("升序后的排列结果为" + l1);

        // 方法九:比较器指定的顺序对指定列表进行排序
        // 通过匿名内部类构建比较器
        Comparator<Integer> comparatorSort = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1; // 从大到小排序
            }
        };
        // 通过比较器进行排序
        Collections.sort(l1, comparatorSort);
        System.out.println("比较器排序后的结果为" + l1);

        // 方法十:交换集合中元素位置
        Collections.swap(l1, 1, 3);
        System.out.println("交换集合中元素位置后的结果为" + l1);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

技术很low的瓜贼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值