zoukankan      html  css  js  c++  java
  • Java针对ArrayList自定义排序的2种实现方法

    Comparator接口可以实现自定义排序,实现Comparator接口时,要重写compare方法: 
      int compare(Object o1, Object o2) 返回一个基本类型的整型 
      如果要按照升序排序,则o1 小于o2,返回-1(负数),相等返回0,01大于02返回1(正数) 
      如果要按照降序排序,则o1 小于o2,返回1(正数),相等返回0,01大于02返回-1(负数)

    (1)让需要进行排序的对象的类实现Comparable接口,重写compareTo(T o)方法,在其中定义排序规则,那么就可以直接调用Collections.sort()来排序对象数组

    public class Student implements Comparable{
      private int id;
      private int age;
      private int height;
      private String name;
      public Student(int id, String name, int age, int height) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.height = height;
      }
      public int getId() {
        return id;
      }
      public int getAge() {
        return age;
      }
      public int getHeight() {
        return height;
      }
      public String getName() {
        return name;
      }
      public void setId(int id) {
        this.id = id;
      }
      public void setAge(int age) {
        this.age = age;
      }
      public void setName(String name) {
        this.name = name;
      }
      public void setHeight(int height) {
        this.height = height;
      }
      @Override
      public int compareTo(Object o) {
        Student s = (Student) o;
        if (this.age > s.age) {
          return 1;
        }
        else if (this.age < s.age) {
          return -1;
        }
        else {
          if (this.height >= s.height) {
            return 1;
          }
          else {
            return -1;
          }
        }
      }
    }

    测试类

    import java.util.*;
    public class Test {
      public static void printData(List<Student> list) {
        for (Student student : list) {
          System.out.println("学号:" + student.getId() + " 姓名:" + student.getName() + " 年龄" + student.getAge() + " 身高:" + student.getHeight());
        }
      }
      public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student(1, "A", 20, 180));
        list.add(new Student(2, "B", 21, 175));
        list.add(new Student(3, "C", 22, 190));
        list.add(new Student(4, "D", 21, 170));
        list.add(new Student(5, "E", 20, 185));
        System.out.println("before sorted");
        printData(list);
        Collections.sort(list);
        System.out.println("after age and height sorted");
        printData(list);
      }
    }

    2)实现比较器接口Comparator,重写compare方法,直接当做参数传进sort中

    public class Student {
      private int id;
      private int age;
      private int height;
      private String name;
      public Student(int id, String name, int age, int height) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.height = height;
      }
      public int getId() {
        return id;
      }
      public int getAge() {
        return age;
      }
      public int getHeight() {
        return height;
      }
      public String getName() {
        return name;
      }
      public void setId(int id) {
        this.id = id;
      }
      public void setAge(int age) {
        this.age = age;
      }
      public void setName(String name) {
        this.name = name;
      }
      public void setHeight(int height) {
        this.height = height;
      }
    }

    测试类

    import java.util.*;
    public class Test {
      public static void printData(List<Student> list) {
        for (Student student : list) {
          System.out.println("学号:" + student.getId() + " 姓名:" + student.getName() + " 年龄" + student.getAge() + " 身高:" + student.getHeight());
        }
      }
      public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student(1, "A", 20, 180));
        list.add(new Student(2, "B", 21, 175));
        list.add(new Student(3, "C", 22, 190));
        list.add(new Student(4, "D", 21, 170));
        list.add(new Student(5, "E", 20, 185));
        System.out.println("before sorted");
        printData(list);
        Collections.sort(list, new Comparator<Student>() {
          @Override
          public int compare(Student o1, Student o2) {
            if(o1.getAge() >= o2.getAge()) {
              return 1;
            }
            else {
              return -1;
            }
          }
        });
        System.out.println("after age sorted");
        printData(list);
        Collections.sort(list, new Comparator<Student>() {
          @Override
          public int compare(Student o1, Student o2) {
            if(o1.getAge() > o2.getAge()) {
              return 1;
            }
            else if (o1.getAge() < o2.getAge()){
              return -1;
            }
            else {
              if (o1.getHeight() >= o2.getHeight()) {
                return 1;
              }
              else {
                return -1;
              }
            }
          }
        });
        System.out.println("after age and height sorted");
        printData(list);
      }
    }
  • 相关阅读:
    单例模式
    设计模式
    C#判断Textbox是否为数字
    C#判断输入的是否是汉字
    C#如何测试代码运行时间
    网上 server2008数据库恢复方法
    C# 控件的缩写
    SQLite主键自增代码
    Sqlite数据库联合查询及表复制等详述
    C#中超链接方法
  • 原文地址:https://www.cnblogs.com/heyjia/p/11338141.html
Copyright © 2011-2022 走看看