java开发jdk1.7下List集合中按照对象中指定字段排序,List集合中按照map中指定字段排序

目录

jdk1.7List集合中按照对象中指定字段排序

jdk1.7List集合中按照map中指定字段排序


jdk1.7List集合中按照对象中指定字段排序

题目:现在有一个list集合、里面存放着多个Student对象,现要求按照Student的score进行排序

代码演示:


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

public class Student {
    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    private String name;
    private int score;

    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 static void main(String[] args) {
        Student student = new Student("张三",60);
        Student student2 = new Student("李四",30);
        Student student3 = new Student("王五",80);
        Student student4 = new Student("赵六",90);
        List<Student> studentList = new ArrayList<>();
        studentList.add(student);
        studentList.add(student2);
        studentList.add(student3);
        studentList.add(student4);
        System.out.println("排序前");
        for (Student o : studentList) {
            System.out.println(o.getName()+"  "+o.getScore());
        }
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                //排序字段   正序    如果需要倒序则将两个值调换
                return o1.getScore()-o2.getScore();   
            }
        });
          /* 或者:
            studentList.sort(new Comparator<Student>() {
                 @Override
                 public int compare(Student o1, Student o2) {

                     return o1.getScore()-o2.getScore();
                 }
     });
     */

        System.out.println("排序后");
        for (Student o : studentList) {
            System.out.println(o.getName()+"  "+o.getScore());
        }

    }
}

结果输出:

jdk1.7List集合中按照map中指定字段排序

题目:现在有一个list集合、里面存放着多个Map,Map中用于存储学生的姓名,语文成绩、数学成绩、英语成绩,现在需要按照语文成绩进行排序输出

代码演示:


import java.util.*;

public class Student {
    public static void main(String[] args) {
       List<Map> studentList = new ArrayList();
    Map map = new HashMap();map.put("姓名","张三");map.put("语文",60);map.put("数学",65);map.put("英语",70);
    studentList.add(map);
        map = new HashMap();map.put("姓名","李四");map.put("语文",80);map.put("数学",40);map.put("英语",50);
        studentList.add(map);
        map = new HashMap();map.put("姓名","王五");map.put("语文",70);map.put("数学",90);map.put("英语",30);
        studentList.add(map);
        System.out.println("排序前");
        for (Map o : studentList) {
            System.out.println(o.get("姓名")+"  "+o.get("语文")+"  "+o.get("数学")+"  "+o.get("英语"));
        }

        Collections.sort(studentList, new Comparator<Map>() {
            @Override
            public int compare(Map o1, Map o2) {
                //排序字段   正序    如果需要倒序则将两个值调换
                return Integer.parseInt(o1.get("语文").toString())-Integer.parseInt(o2.get("语文").toString());
            }
        });
        System.out.println("排序后");
        for (Map o : studentList) {
            System.out.println(o.get("姓名")+"  "+o.get("语文")+"  "+o.get("数学")+"  "+o.get("英语"));
        }
    }
}

结果输出:

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值