java比较器 之compareable 和comparato比较

compareable
测试类import java.util.Set;
import java.util.TreeSet;

public class Test {

    public static void main(String[] args) {
        Set<Person>allset = new TreeSet<Person>();
        allset.add(new Person(22,"hjw",20000.0,"论语",50.0));
        allset.add(new Person(22,"fyy",7000.0,"大学",40.0));
        allset.add(new Person(22,"hjl",6000.0,"孟子",30.0));
        allset.add(new Person(22,"hlq",10000.0,"中庸",20.0));
        allset.add(new  Person(24, "hsx", 20000.0,"诗经", 40.0));
        allset.add(new Person(22,"cl",20000.0,"离骚",10.0));
        allset.add(new Person(22,"zsk",20000.0,"墨子",25.0));
        allset.add(new Person(22,"hjw",20000.0,"论语",23.0));
        allset.add(new Person(22,"hjw",20000.0,"论语",22.0));
        System.out.println(allset);
    }

}

person类
public class Person extends Book implements Comparable<Person> {
    private Integer age;
    private String name;
    private Double salary;
    //private String book;
    public Integer getAge() {
        return age;
    }
    public Person(Integer age, String name, Double salary,String bookname, double price) {
        super(bookname, price);
        this.age = age;
        this.name = name;
        this.salary = salary;
        //this.book = book;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    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(Person o) {

        if (this.salary>o.salary) {
            return -1;
        }
        else if(this.salary<o.salary){
            return 1;
        }
        else
        {
            if (this.age>o.age){
                return 1;
            }else if(this.age<o.age){
                return -1;
            }else{
                if (this.name!=o.name) {
                    return this.name.compareTo(o.name);
                }else {
                    if(this.price>o.price){
                        return 1;
                    }else if(this.price<o.price){
                        return -1;
                    }else {
                        return this.bookname.compareTo(o.bookname);
                    }
                }
            }
        }
    }
    @Override
    public String toString() {
        return "Person [age=" + age + ", name=" + name + ", salary=" + salary + ", bookname=" + bookname + ", price="
                + price + "]";
    }

}
Book类
public class Book {
    public String bookname;
    public Double price;

    public Book(String bookname, double price) {

        this.bookname = bookname;
        this.price = price;
    }
    public String getBookname() {
        return bookname;
    }
    public void setBookname(String bookname) {
        this.bookname = bookname;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Book [bookname=" + bookname + ", price=" + price + "]";
    }

}

自定义比较器Comparator类

Book类
public class Book {
    public String bookname;
    public Double price;
    public Book(){}
    public Book(String bookname, double price) {

        this.bookname = bookname;
        this.price = price;
    }
    public String getBookname() {
        return bookname;
    }
    public void setBookname(String bookname) {
        this.bookname = bookname;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Book [bookname=" + bookname + ", price=" + price + "]";
    }

}
Person类
public class Person {
    private Integer age;
    private String name;
    private double salary;
    private Book book;
    public Person(Integer age, String name, double salary,String bookname, double price) {

        this.age = age;
        this.name = name;
        this.salary = salary;
        this.book=new Book(bookname,price);

    }
    public Integer getAge() {return age;}
    public void setAge(Integer age) {
        this.age = age;
    }
    public Book getBook() {
        return book;
    }
    public void setBook(Book book) {
        this.book = book;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getSalary() {
        return salary;
    }

    @Override
    public String toString() {
        return "Person [age=" + age + ", name=" + name + ", salary=" + salary + ", book=" + book + "]";
    }

}
自定义Comparator类
import java.util.Comparator;

public class SalaryCom implements Comparator<Person>{

    @Override
    public int  compare(Person o1, Person o2) {
        if(o1.getSalary()!=o2.getSalary()){
        return (int) -(o1.getSalary()-o2.getSalary());
        }else if(o1.getAge()!=o2.getAge()){
            return (int) (o1.getAge()-o2.getAge());
        }else if(o1.getName()!=o2.getName()){
            return o1.getName().compareTo(o2.getName());}
            else{
                if(o1.getBook().price!=o2.getBook().price){
                    return (int) (o1.getBook().price-o2.getBook().price);
                }else{
                    return o1.getBook().bookname.compareTo(o2.getBook().bookname);
                }

            }
        }
    }
Test类
import java.util.Set;
import java.util.TreeSet;
public class Test {

    public static void main(String[] args) {
        SalaryCom s =new SalaryCom();
        Set<Person> allset = new TreeSet<Person>(s);

        allset.add(new Person(22,"hjw",20000,"论语",50.0));
        allset.add(new Person(22,"fyy",7000,"大学",40.0));
        allset.add(new Person(22,"hjl",6000,"孟子",30.0));
        allset.add(new Person(22,"hlq",10000,"中庸",20.0));
        allset.add(new  Person(24,"hsx",20000,"诗经", 40.0));
        allset.add(new Person(22,"cl",20000,"离骚",10.0));
        allset.add(new Person(22,"zsk",20000,"墨子",25.0));
        allset.add(new Person(22,"hj",20000,"论语",50.0));
        allset.add(new Person(22,"hjw",20000,"论语",22.0));
        System.out.println(allset);
    }

}

使用匿名内部类自定义比较器Comparator

Book类

public class Book {
    public String bookname;
    public Double price;
    public Book(){}
    public Book(String bookname, double price) {

        this.bookname = bookname;
        this.price = price;
    }
    public String getBookname() {
        return bookname;
    }
    public void setBookname(String bookname) {
        this.bookname = bookname;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Book [bookname=" + bookname + ", price=" + price + "]";
    }

}
Person类
public class Person {
    private Integer age;
    private String name;
    private double salary;
    private Book book;
    public Person(Integer age, String name, double salary,String bookname, double price) {

        this.age = age;
        this.name = name;
        this.salary = salary;
        this.book=new Book(bookname,price);

    }
    public Integer getAge() {return age;}
    public void setAge(Integer age) {
        this.age = age;
    }
    public Book getBook() {
        return book;
    }
    public void setBook(Book book) {
        this.book = book;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getSalary() {
        return salary;
    }

    @Override
    public String toString() {
        return "Person [age=" + age + ", name=" + name + ", salary=" + salary + ", book=" + book + "]";
    }

}
Test测试类
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

import com.qianfeng.homework02.Person;

public class Test {

    public static void main(String[] args) {
    Set<Person> allset = new TreeSet<Person>(new Comparator<Person>() {

        @Override
        public int compare(Person o1, Person o2) {
            if(o1.getSalary()!=o2.getSalary()){
                return (int) -(o1.getSalary()-o2.getSalary());
                }else if(o1.getAge()!=o2.getAge()){
                    return (int) (o1.getAge()-o2.getAge());
                }else if(o1.getName()!=o2.getName()){
                    return o1.getName().compareTo(o2.getName());}
                    else{
                        if(o1.getBook().price!=o2.getBook().price){
                            return (int) (o1.getBook().price-o2.getBook().price);
                        }else{
                            return o1.getBook().bookname.compareTo(o2.getBook().bookname);
                        }

                    }
        }
    });

        allset.add(new Person(22,"hjw",20000,"论语",50.0));
        allset.add(new Person(22,"fyy",7000,"大学",40.0));
        allset.add(new Person(22,"hjl",6000,"孟子",30.0));
        allset.add(new Person(22,"hlq",10000,"中庸",20.0));
        allset.add(new  Person(24,"hsx",20000,"诗经", 40.0));
        allset.add(new Person(22,"cl",20000,"离骚",10.0));
        allset.add(new Person(22,"zsk",20000,"墨子",25.0));
        allset.add(new Person(22,"hj",20000,"论语",50.0));
        allset.add(new Person(22,"hjw",20000,"论语",22.0));
        System.out.println(allset.size());
        System.out.println(allset);

    }

}

注意:String 字符串类的Comparator已经实现了compareTo方法,可以直接进行调用比较!

				
时间: 2024-11-06 09:25:07

java比较器 之compareable 和comparato比较的相关文章

Java 比较器的用法

第一次写博客,正好在回顾Java的时候用到了比较器,记录一下使用的方法. Java比较器多用于对象数组的排序,主要用到comparable和comparator接口 1.使用comparable接口 首先将需要实现排序对象的类实现comparable接口,实现后覆写comparaTo(T other)方法,在comparaTo方法中写出比较规则,最后调用java.utils.Arrays.sort()方法传入需要比较的对象数组即可排序. 测试如下: 1 import java.util.Arra

java比较器Comparable接口和Comaprator接口

java的比较器有两类,分别是Comparable接口和Comparator接口. 在为对象数组进行排序时,比较器的作用非常明显,首先来讲解Comparable接口. 让需要进行排序的对象实现Comparable接口,重写其中的compareTo(T o)方法,在其中定义排序规则,那么就可以直接调用java.util.Arrays.sort()来排序对象数组,实例如下: class Student implements Comparable<Student>{ private String n

Java中equals与==和comparaTo的区别

一.先说说Java中equals和==的区别: Java中的数据类型,可分为两类: 1.基本数据类型(也叫原始数据类型) 八大基本数据类型 char byte short int long double float boolean 2.引用数据类型(也就复合数据类型)除了上面的8个,其他都是引用数据类型 区别: 基本数据类型比较只能用==,不能用equals,因为用equals(参数),参数必须为对象. 他们之间的比较,比较的是他们的值. public class Test { public s

java比较器Comparator 使用

PresonDemo package cn.stat.p5.person.demo; public class PresonDemo implements Comparable { private String name; private int age; /** * @param args */ public String getName() { return name; } public void setName(String name) { this.name = name; } publ

JAVA比较器:Comparable和Comparator

一.Comparable接口 1.public interface Comparable{ public int compareTo(Object other); } 2.当本对象小于.等于或大于other对象时,相应返回一个小于.等于或大于0的值. 3.若对象不可比较,抛出ClassCastException 4.compareTo()定义的顺序是类的自然顺序,即此排序对类的对象来说是最自然的. 5.equals()定义一种自然相等关系,两个对象相等,返回ture. 6.许多类:String.

java比较器Comparable接口和Comaprator接口的比较

Comparable接口:让自定义的对象具有比较规则 import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; /* * Comparable接口:让自定义的对象具有比较规则 * * */ public class ComparableDemo { public static void main(String[] args) { ArrayList<Student> list =

Java 比较器

比较器 Arrays 类 主要功能: 完成所有与数组有关的操作的工具类 二分查找: 在一个有序的数字序列中进行二分查找 public static int binarySearch(数据类型 [] a , 数据类型 key) 案例实现 public class TestDemo { public static void main(String [] args) throws ParseException { int date [] = new int [] {1,4,2,5,7,4,3,8} ;

java比较器

1 package com.sun; 2 3 public class ComparatorDemo implements Comparable<ComparatorDemo> { 4 5 private String name; 6 private Integer age; 7 private float score; 8 9 public ComparatorDemo(String name, Integer age, float score) { 10 super(); 11 this.

java比较器的复用

博客主页:http://blog.csdn.net/minna_d 设想这么一种场景:A有N个字段,也有专门对A的比较函数.每一次比较函数,在N多个业务线复用. 那么,问题来了, 突然有一天A多加了一个字段in,而且在原一特定场景中这个字段比其它所有字段的优先级都应该高.在其它场景中又没有影响. 该怎么解决这个问题? 1. 重写原有的所有的Comparator类,重写它们的compare方法.这种方法代价太大,因为N多地方需要 if...else 2. 只正对特定场景新增一个组合之前的compa