温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java中怎么对arrayList按数字大小逆序排序

发布时间:2023-05-06 10:51:13 来源:亿速云 阅读:97 作者:zzz 栏目:开发技术

这篇文章主要介绍“java中怎么对arrayList按数字大小逆序排序”,在日常操作中,相信很多人在java中怎么对arrayList按数字大小逆序排序问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”java中怎么对arrayList按数字大小逆序排序”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

    对arrayList按数字大小逆序排序

    对集合排序要用Collections.sort方法,由于默认它是按从小到大的排序的,如果我们需要逆序的,那么就需要实现Comparator接口的compare方法来完成自定义排序。

    需要注意Comparator是接口,new Comparator(){} 的作用是new了一个实现接口的匿名类,开发人员需要在匿名类内部(花括号内)实现你那个接口。

    代码

    public static void main(String[] args) {
            Integer[] nums = {1,5,34,6,8,7,33};
            ArrayList<Integer> numberList = new ArrayList<>();
            Collections.addAll(numberList, nums);
    
            // 排序前
            System.out.println("逆序前 numberList " + numberList);
    
            // 排序后
            ArrayList<Integer> copyList = new ArrayList<>(numberList);
            Collections.sort(copyList, new Comparator<Integer>() {
                @Override
                public int compare(Integer num1, Integer num2) {
                    if (num1 > num2) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            });
            System.out.println("逆序后 copyList " + copyList);
            // 原列表不变
            System.out.println("逆序后 numberList " + numberList);
        }

    arrayList实现自定义排序

    ArrayList排序使用

    ArrayList中存在sort排序方法,只要你实现了Comparator的接口,按照你自己的排序业务进行实现,你只要告诉这个接口按照什么类型进行排序就OK了。这种方式类似于设计模式中的策略模式,把流程划分好,具体的业务逻辑由用户指定

    代码实现:

    public class ComparatorTest {
        public static void main(String[] args) {
            baseTypeSort();
            referenceTypeSort();
    
        }
    
        private static void referenceTypeSort() {
            Person p1 = new Person(10);
            Person p2 = new Person(16);
            Person p3 = new Person(1);
            Person p4 = new Person(8);
            Person p5 = new Person(100);
    
            List<Person> people = new ArrayList<>();
            people.add(p1);
            people.add(p2);
            people.add(p3);
            people.add(p4);
            people.add(p5);
            System.out.println("排序前:" + people);
            people.sort((o1, o2) -> o2.getAge() - o1.getAge());
            System.out.println("降序:" + people);
            Collections.sort(people, (o1, o2) -> o1.getAge() - o2.getAge());
            System.out.println("升序:" + people);
            people.sort(Comparator.comparing(Person::getAge));
            System.out.println("comparing写法升序:" + people);
            people.sort(Comparator.comparing(Person::getAge).reversed());
            System.out.println("comparing写法降序:" + people);
        }
    
        private static void baseTypeSort() {
            // 初始化一组数据,这组数据可以是任意对象
            int[] data = {7, 5, 1, 2, 6, 8, 10, 12, 4, 3, 9, 11, 13, 15, 16, 14};
            // 构建成一个集合
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < data.length; i++) {
                list.add(data[i]);
            }
            System.out.println("排序前:" + list);
            //逆序
            list.sort((o1, o2) -> o2 - o1);
            System.out.println("降序:" + list);
        }
    }

    由于现在主流jdk都升级到1.8以上,所以使用lamda表达式实现,这里简单介绍一下lamda表达式使用:

    • 以逗号分隔,以()关闭的形参:(Integer m, Integer n)

    • 箭头标记:->

    • 主体部分则是一个单表达式或者声明代码块。

    • 如下是单表达式形式:

    (o1, o2) -> o2.getAge() - o1.getAge()

    注意点:

    • Java7,list并没有sort方法,请使用Collections.sort(),Collections.sort()传入ArrayList和自己实现Commparator接口的类的对象,实现自定义排序

    • 使用Collections.sort()传入ArrayList和自己实现Commparator接口的类的对象,实现自定义排序

    • 使用List.sort()传入自己实现Commparator接口的类的对象,实现自定义排序

    • Comparator返回值在jdk1.7、jdk1.8里必须是一对相反数,可以使用差值简化写法,正数表示升序,负数表示降序

    • ArrayList中的sort排序是采用归并排序的,当数组中的数据非常大的时候,会采用几次归并来完成排序.具体采用几次归并,会通过相关方法进行计算

    原理分析

    Collections.sort方法底层就是调用的Arrays.sort方法,而Arrays.sort底层调用了一个非常优秀的工程排序实现算法:TimSort,Timsort是一种结合了归并排序和插入排序的混合算法,由Tim Peters在2002年提出,并且已经成为Python 2.3版本以后内置排序算法。

    在数据量小的时候使用插入排序,虽然插入排序的时间复杂度是O(n^2),但是它的常数项比较小,在数据量较小的时候具备较快的速度。

    在数据量较大的时候,如果是基本类型,使用快速排序,如果是引用类型使用归并排序。这是因为快速排序是不稳定的,而插入排序和归并排序都是稳定性排序。

    到此,关于“java中怎么对arrayList按数字大小逆序排序”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI