温馨提示×

温馨提示×

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

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

Java针对ArrayList自定义排序的2种实现方法

发布时间:2020-09-12 23:18:00 来源:脚本之家 阅读:121 作者:_TCgogogo_ 栏目:编程语言

本文实例讲述了Java针对ArrayList自定义排序的2种实现方法。分享给大家供大家参考,具体如下:

Java中实现对list的自定义排序主要通过两种方式

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);
  }
}

结果:

before sorted
学号:1 姓名:A 年龄20 身高:180
学号:2 姓名:B 年龄21 身高:175
学号:3 姓名:C 年龄22 身高:190
学号:4 姓名:D 年龄21 身高:170
学号:5 姓名:E 年龄20 身高:185
after age and height sorted
学号:1 姓名:A 年龄20 身高:180
学号:5 姓名:E 年龄20 身高:185
学号:4 姓名:D 年龄21 身高:170
学号:2 姓名:B 年龄21 身高:175
学号:3 姓名:C 年龄22 身高:190

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);
  }
}

输出结果:

before sorted
学号:1 姓名:A 年龄20 身高:180
学号:2 姓名:B 年龄21 身高:175
学号:3 姓名:C 年龄22 身高:190
学号:4 姓名:D 年龄21 身高:170
学号:5 姓名:E 年龄20 身高:185
after age sorted
学号:1 姓名:A 年龄20 身高:180
学号:5 姓名:E 年龄20 身高:185
学号:2 姓名:B 年龄21 身高:175
学号:4 姓名:D 年龄21 身高:170
学号:3 姓名:C 年龄22 身高:190
after age and height sorted
学号:1 姓名:A 年龄20 身高:180
学号:5 姓名:E 年龄20 身高:185
学号:4 姓名:D 年龄21 身高:170
学号:2 姓名:B 年龄21 身高:175
学号:3 姓名:C 年龄22 身高:190

单从上面的例子可以看出排序是稳定的,去看了下java的Collections.sort的源代码,确实是基于稳定的归并排序实现的,内部还做了优化,叫TimSort。(关于TimSort还可参考https://baike.baidu.com/item/TimSort?fr=aladdin)

PS:这里再为大家推荐一款关于排序的演示工具供大家参考:

在线动画演示插入/选择/冒泡/归并/希尔/快速排序算法过程工具:
http://tools.jb51.net/aideddesign/paixu_ys

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

向AI问一下细节

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

AI