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);
      }
    }
  • 相关阅读:
    STM32中GPIO的8种工作模式
    robots.txt与搜索引擎
    关于《腾讯工具类APP的千年老二》的读后感
    PCB布线的操作步骤
    c语言数据库编程ODBC
    锂电池相关结构优势特点及其保护电路解析方案
    C语言中的#与##字符的作用
    PADS中Layer的描述说明
    吃了单片机GPIO端口工作模式的大亏——关于强推挽输出和准双向口(弱上拉)的实际应用
    Protel与PADS之间相关文件的转换
  • 原文地址:https://www.cnblogs.com/heyjia/p/11338141.html
Copyright © 2011-2022 走看看