List集合排序

创建实体类student,添加setter和getter方法

package Java.Sort.entity;

/**
 * @Description:
 * @Author: at
 * @Date: 2021/9/13 17:27
 */
public class Student {
    private String name;
    private int score;
    private int height;
    private String college;
    private String address;

    public Student() {

    }

    public Student(String name, int score, int height, String college, String address) {
        this.name = name;
        this.score = score;
        this.height = height;
        this.college = college;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public String getCollege() {
        return college;
    }

    public void setCollege(String college) {
        this.college = college;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", score=" + score +
                ", height=" + height +
                ", major='" + college + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

1.单属性变量List排序

1.1按照变量属性升序,降序排序

public class singleVariableSort1 {
    /**
     * 单属性变量list 自身属性升序, 降序排序
     */
    private void singleVariableSort1(){

        List<Integer> list = Arrays.asList(10,1,6,4,8,7,9,3,2,5);

        System.out.println("原始数据:");
        list.forEach(n ->{System.out.print(n+", ");});

        System.out.println("");
        System.out.println("升序排列:");
        Collections.sort(list); // 升序排列
        list.forEach(n ->{System.out.print(n+", ");});

        System.out.println("");
        System.out.println("降序排列:");
        Collections.reverse(list); // 倒序排列
        list.forEach(n ->{System.out.print(n+", ");});
    }

    public static void main(String[] args) {
        new singleVariableSort1().singleVariableSort1();
    }
}

在这里插入图片描述

1.2按照自定义的顺序排序

public class singleVariableSort2 {
    private void singleVariableSort2(){
        List<String> list = Arrays.asList("北京","上海","北京","广州","广州","上海","北京","上海");
        List<String> sortRule=Arrays.asList("北京","上海","广州");
        System.out.println("原始数据");
        list.forEach(n->{System.out.print(n+", ");});
        System.out.println("");
        System.out.println("排序规则");
        sortRule.forEach(n->{System.out.print(n+", ");});

        System.out.println("");
        System.out.println("正序排序");
        Collections.sort(list, new Comparator<String>() {
            public int compare(String o1, String o2) {
                int io1=sortRule.indexOf(o1);
                int io2=sortRule.indexOf(o2);
                return io1-io2;
            }
        });
        list.forEach(n->{System.out.print(n+", ");});

        System.out.println("");
        System.out.println("倒序排序");
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int io1= sortRule.indexOf(o1);
                int io2= sortRule.indexOf(o2);
                return io2-io1;
            }
        });
        list.forEach(n->{System.out.print(n+", ");});
    }

    public static void main(String[] args) {
        new singleVariableSort2().singleVariableSort2();
    }
}

在这里插入图片描述

2.对象List排序

2.1按照对象单属性升序,降序排序

public class entitySort1 {
    private void entitySort1() {

        //Student 的 list 集合
        List<Student> students = new ArrayList<>();
        students.add(new Student("张三",90,180,"电气学院","北京"));
        students.add(new Student("李四",80,165,"计算机学院","上海"));
        students.add(new Student("王五",91,170,"财经学院","上海"));
        students.add(new Student("赵明",80,182,"计算机学院","北京"));
        students.add(new Student("钱海",75,181,"计算机学院","广州"));
        students.add(new Student("孙理",82,172,"财经学院","上海"));
        students.add(new Student("周伟",90,168,"电气学院","广州"));
        students.add(new Student("郑亮",80,178,"财经学院","广州"));

        System.out.println("原始数据:");
        students.forEach(s ->{
            System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+ s.getCollege()+""+s.getAddress());});

        System.out.println("按照分数升序排序:");
        students.sort(Comparator.comparing(Student::getScore));
        students.forEach(s ->{
            System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+ s.getCollege()+""+s.getAddress());});

        System.out.println("按照分数降序排序:");
        students.sort(Comparator.comparing(Student::getScore).reversed());
        students.forEach(s ->{
            System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());});

    }

    public static void main(String[] args) {
       new  entitySort1().entitySort1();
    }
}

在这里插入图片描述

2.2按照对象多属性升序,降序排序

public class entitySort2 {
    private void entitySort2(){

        //Student 的 list 集合
        List<Student> students = new ArrayList<>();
        students.add(new Student("张三",90,180,"电气学院","北京"));
        students.add(new Student("李四",80,165,"计算机学院","上海"));
        students.add(new Student("王五",91,170,"财经学院","上海"));
        students.add(new Student("赵明",80,182,"计算机学院","北京"));
        students.add(new Student("钱海",75,181,"计算机学院","广州"));
        students.add(new Student("孙理",82,172,"财经学院","上海"));
        students.add(new Student("周伟",90,168,"电气学院","广州"));
        students.add(new Student("郑亮",80,178,"财经学院","广州"));

        System.out.println("原始数据:");
        students.forEach(s ->{
            System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());});

        System.out.println("按照分数降序排序,当分数相同时, 按照身高升序排序");
        students.sort(Comparator.comparing(Student::getScore).reversed().thenComparing(Student::getHeight));
        students.forEach(s ->{
            System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());});

        System.out.println("按照分数降序排序,当分数相同时, 按照身高降序排序");
        Collections.sort(students, new Comparator<Student>()
        {
            public int compare(Student student1, Student student2)
            {
                if(student1.getScore()==(student2.getScore())){
                    return student2.getHeight() - student1.getHeight();
                }else{
                    return student2.getScore() - student1.getScore();
                }
            }
        });

        students.forEach(s ->{
            System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+""+s.getAddress());});
    }

    public static void main(String[] args) {
        new entitySort2().entitySort2();;
    }
}

在这里插入图片描述

2.3按照对象自定义单属性的顺序排序

public class entitySort3 {
    private void entitySort3(){
        List<Student> students = new ArrayList<>();
        students.add(new Student("张三",90,180,"电气学院","北京"));
        students.add(new Student("李四",80,165,"计算机学院","上海"));
        students.add(new Student("王五",91,170,"财经学院","上海"));
        students.add(new Student("赵明",80,182,"计算机学院","北京"));
        students.add(new Student("钱海",75,181,"计算机学院","广州"));
        students.add(new Student("孙理",82,172,"财经学院","上海"));
        students.add(new Student("周伟",90,168,"电气学院","广州"));
        students.add(new Student("郑亮",80,178,"财经学院","广州"));

        students.forEach(s ->{System.out.println(s.getName()+" "+s.getScore()+" "+s.getHeight()+" "+s.getCollege()+" "+s.getAddress());});

        System.out.println("自定义按照地区(北京,上海,广州)排序:");
        List<String> addressOrder= Arrays.asList("北京","上海","广州");
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int io1= addressOrder.indexOf(s1.getAddress());
                int io2= addressOrder.indexOf(s2.getAddress());
                return io1-io2;
            }
        });
        students.forEach(s->{System.out.println(s.getName()+" "+ s.getScore()+" "+s.getHeight()+" "+s.getCollege()+" "+s.getAddress());});
    }
    public static void main(String[] args) {
        new entitySort3().entitySort3();
    }
}

在这里插入图片描述

3、对日期进行排序

package Java.Sort;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * @Description:
 * @Author: XiaoMu
 * @Date: 2021/12/16 10:13
 */
public class DateSort {
    public static void main(String[] args) {
        ArrayList<String> timeList = new ArrayList<String>();
        timeList.add("2013-7-20 20:20");
        timeList.add("2013-7-21 10:20");
        timeList.add("2013-7-25 20:20");
        timeList.add("2013-7-22 20:20");
        timeList.add("2013-7-23 20:20");
        timeList.add("2013-7-24 20:21");
        timeList.add("2013-7-25 20:20");
        System.out.println(timeList);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-M-dd HH:mm");
        ArrayList<Date> dateList = new ArrayList<Date>();
        /**
         * 字符串转时间
         */
        for (String str : timeList) {
            try {
                dateList.add(format.parse(str));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        /**
         * 打印时间
         */
        System.out.println("排序前:");
        for(Date d:dateList){
            System.out.println(format.format(d));
        }
        /**
         * 冒泡排序
         * 如果不喜欢可以使用其他的排序方法
         */
        Date tempDate = null;
        for (int i = dateList.size()- 1; i > 0; --i) {
            for (int j = 0; j < i; ++j) {
                /**
                 * 从大到小的排序
                 */
                if(dateList.get(j+1).after(dateList.get(j))){
                    tempDate = dateList.get(j);
                    dateList.set(j, dateList.get(j+1));
                    dateList.set(j+1, tempDate);
                }else{
                    /**
                     * 从小到大
                     */
//            		tempDate = dateList.get(j);
//            		dateList.set(j, dateList.get(j+1));
//            		dateList.set(j+1, tempDate);
                }
            }
        }
        /**
         * 打印排序之后的时间
         */

        System.out.println("排序后:");
        for(Date d:dateList){
            System.out.println(format.format(d));
        }
    }
}

4、使用stream流

public class demo {
    public static void main(String[] args) {
        //Student 的 list 集合
        List<Student> list = new ArrayList<>();
        list.add(new Student("张三", 90, 180, "电气学院", "北京"));
        list.add(new Student("李四", 80, 165, "计算机学院", "上海"));
        list.add(new Student("王五", 91, 170, "财经学院", "上海"));
        list.add(new Student("赵明", 80, 182, "计算机学院", "北京"));
        list.add(new Student("钱海", 75, 181, "计算机学院", "广州"));
        list.add(new Student("孙理", 82, 172, "财经学院", "上海"));
        list.add(new Student("周伟", 90, 168, "电气学院", "广州"));
        list.add(new Student("郑亮", 80, 178, "财经学院", "广州"));
		//使用stream流筛选出list对象中分数大于80的student,返回List对象
        List<Student> collect = list.stream().filter(student -> student.getScore() > 80).collect(Collectors.toList());
        collect.forEach(System.out::println);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小蜜蜂127

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

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

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

打赏作者

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

抵扣说明:

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

余额充值