java中对象的1排序_Java中对象的排序问题笔记

1、基本类型数据,可以直接排序

可以使用Arrays.sort()对int、double、long等基本类型的数组进行排序,也包括String对象。

import java.util.Arrays;

public class SortDemo {

public static void main(String[] args) {

// String和基本类型对象排序

String[] str=new String[] {"ad","abc","bd","dc","bb"};

int[] number=new int[] {8,17,11,0,-7,9};

for(var t:str) {

System.out.print(t+"\t");

}

System.out.println();

for(var t:number) {

System.out.print(t+"\t");

}

Arrays.sort(str);

Arrays.sort(number);

System.out.println();

System.out.println("================排序后================");

for(var t:str) {

System.out.print(t+"\t");

}

System.out.println();

for(var t:number) {

System.out.print(t+"\t");

}

}

}

06468e3310c3bff37698a3e237dca47d.png

2、非基本类型对象排序方法

b7333cd1db7966e9c8136240557fd996.png

public interface Comparable 该接口对实现它的每个类的对象强加一个整体排序。 这个排序被称为类的自然排序 ,类的compareTo方法被称为其自然比较方法 。

Comparable是排序接口。若一个类实现了Comparable接口,就意味着该类支持排序。实现了Comparable接口的类的对象的列表或数组可以通过Collections.sort或Arrays.sort进行自动排序。

所以,我们可以给该类编写一个排序方法,方法就是继承并实现Comparable接口

import java.util.Arrays;

//学生类

class Student implements Comparable{

private String name;

private int age;

public Student(String name, int age) {

this.name = name;

this.age = age;

}

public String getName() {

return this.name;

}

@Override

public int compareTo(Student o) {

return this.age-o.age;

}

}

public class SortDemo {

public static void main(String[] args) {

// 学生类对象排序

Student[] obj=new Student[] {

new Student("老三",14),new Student("老二",15),

new Student("老五",13),new Student("老大",16),

new Student("老六",12),new Student("老四",13)};

for(var t:obj) {

System.out.print(t.getName()+"\t");

}

System.out.println();

Arrays.sort(obj);

System.out.println("================排序后================");

for(var t:obj) {

System.out.print(t.getName()+"\t");

}

}

}

dcedfc4da1d19b14dc3067e66616af0a.png

Comparator简介

Comparator是比较接口,我们如果需要控制某个类的次序,而该类本身不支持排序(即没有实现Comparable接口),那么我们就可以建立一个“该类的比较器”来进行排序,这个“比较器”只需要实现Comparator接口即可。也就是说,我们可以通过实现Comparator来新建一个比较器,然后通过这个比较器对类进行排序。

import java.util.Arrays;

import java.util.Comparator;

//学生类

class Student{

private String name;

private int age;

public Student(String name, int age) {

this.name = name;

this.age = age;

}

public String getName() {

return this.name;

}

public int getAge() {

return this.age;

}

}

//比较器类

class StudentCompartor implements Comparator{

@Override

public int compare(Student o1, Student o2) {

return o1.getAge()-o2.getAge();

}

}

public class SortDemo {

public static void main(String[] args) {

// 学生类对象排序

Student[] obj=new Student[] {

new Student("老三",14),new Student("老二",15),

new Student("老五",13),new Student("老大",16),

new Student("老六",12),new Student("老四",13)};

for(var t:obj) {

System.out.print(t.getName()+"\t");

}

System.out.println();

Arrays.sort(obj,new StudentCompartor());

System.out.println("================排序后================");

for(var t:obj) {

System.out.print(t.getName()+"\t");

}

}

}

267fe393b36b508abd99db0b448724c7.png

3、Comparable和Comparator区别比较

Comparable是排序接口,若一个类实现了Comparable接口,就意味着“该类支持排序”。而Comparator是比较器,我们若需要控制某个类的次序,可以建立一个“该类的比较器”来进行排序。

Comparable相当于“内部比较器”,而Comparator相当于“外部比较器”。

两种方法各有优劣, 用Comparable 简单, 只要实现Comparable 接口的对象直接就成为一个可以比较的对象,但是需要修改源代码。

用Comparator 的好处是不需要修改源代码, 而是另外实现一个比较器, 当某个自定义的对象需要作比较的时候,把比较器和对象一起传递过去就可以比大小了, 并且在Comparator 里面用户可以自己实现复杂的可以通用的逻辑,使其可以匹配一些比较简单的对象,那样就可以节省很多重复劳动了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值