内部比较器
2021/6/29 23:50:49
本文主要是介绍内部比较器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.让对象所在的类,实现Comparable接口
2.方法重写compareTo();
3.完成比较规则的制定
对象比较
根据age去比
public class Person implements Comparable { String name; int age; double salary; public Person() { } public Person(String name, int age, double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}'; } @Override public int compareTo(Object o) { Person p2 = (Person)o; return this.age-p2.age; } }
public class Test { public static void main(String[] args) { Person p1 = new Person("李白",20,999.9); Person p2 = new Person("杜甫",30,999.8); int i = p1.compareTo(p2); System.out.println(i); } }
根据薪水去比
package com.atguigu.Compare.c1; public class Person implements Comparable { String name; int age; double salary; public Person() { } public Person(String name, int age, double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", salary=" + salary + '}'; } @Override public int compareTo(Object o) { Person p = (Person)o; if (this.salary>p.salary){ return 1; }else if(this.salary<p.salary){ return -1; }else{ return 0; } //return Double.compare(this.salary, p.salary); } }
public class Test { public static void main(String[] args) { Person p1 = new Person("李白",20,999.9); Person p2 = new Person("杜甫",30,999.8); int i = p1.compareTo(p2); System.out.println(i); } }
数组比较
按照薪资比较
import com.atguigu.innerclass.in4.exer.Emplyee; public class Employee implements Comparable{ int id; String name; double salary; public Employee() { } public Employee(int id, String name, double salary) { this.id = id; this.name = name; this.salary = salary; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", salary=" + salary + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public int compareTo(Object o) { Employee e = (Employee)o; /*按照薪资比较*/ return Double.compare(this.salary, e.salary); } }
import com.atguigu.innerclass.in4.exer.Emplyee; import java.util.concurrent.ForkJoinPool; public class Test { public static void main(String[] args) { Employee e1 = new Employee(1,"张三",9999.9); Employee e2 = new Employee(6,"李四",9999.1); Employee e3 = new Employee(3,"王五",9999.3); Employee e4 = new Employee(4,"赵六",9999.6); Employee e5 = new Employee(2,"陈七",9999.8); Employee [] es = {e1,e2,e3,e4,e5}; /*利用冒泡排序*/ for (int i =0;i<es.length-1;i++){ for(int j=0;j<es.length-1-i;j++){ if (es[j].compareTo(es[j + 1])>0) { Employee temp = es[j]; es[j]=es[j+1]; es[j+1] = temp; } } } for (Employee ele : es){ System.out.println(ele); } } }
薪资相同时候,按照id比
只需要修改Comparable的重写方法:compareTo
public int compareTo(Object o) { Employee e = (Employee)o; /*薪资相同,按照id比较*/ if(this.salary==e.salary){ return this.id - e.id; } return Double.compare(this.salary, e.salary); }
数组通用工具类比较
冒泡排序换为,也需要重写compareTo方法,制定比较规则。
Arrays.sort(es);
这篇关于内部比较器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-28微服务架构中API版本控制的实践
- 2024-09-28AI给的和自己写的Python代码,都无法改变输入框的内容,替换也不行
- 2024-09-27Sentinel配置限流资料:新手入门教程
- 2024-09-27Sentinel配置限流资料详解
- 2024-09-27Sentinel限流资料:新手入门教程
- 2024-09-26Sentinel限流资料入门详解
- 2024-09-26Springboot框架资料:初学者入门教程
- 2024-09-26Springboot框架资料详解:新手入门教程
- 2024-09-26Springboot企业级开发资料:新手入门指南
- 2024-09-26SpringBoot企业级开发资料新手指南