排序 Comparator Comparable

一:Comparable:實現Comparable無參接口,重寫equals()和hashCode()方法,重寫compareTo(Object o)方法;

二:Comparator:實現Comparator有參接口,重寫compare()方法

三:Comparator 與 Comparable共存時,Comparator優先

package com.mall.controllor.alene;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Created by 60341 on 2020/3/23.
 */
public class Customer implements Comparable{

    private String name;

    private int age;

    public Customer(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

    @Override
    public int compareTo(Object o) {
        Customer other = (Customer) o;
        //按照姓名排序
        if (this.name.compareTo(other.getName())>0) {
            return 1;
        }
        if (this.name.compareTo(other.getName()) < 0){
            return -1;
        }
        //按照年齡排序
        if (this.age > other.getAge()){
            return 1;
        }
        if (this.age < other.getAge()){
            return -1;
        }
        return 0;
    }
    @Override
    public boolean equals(Object obj){
        if (this == obj){
            return true;
        }
        if (! (obj instanceof Customer)){
            return false;
        }
        final Customer other = (Customer)obj;
        if (this.name == other.getName() && this.age == other.getAge()){
            return true;
        }else {
            return false;
        }
    }
    @Override
    public int hashCode(){
        int result;
        result = (name == null? 0: name.hashCode());
        result = result*29 + age;
        return result;
    }

    public static void main(String[] args) {
        Set<Customer> set = new HashSet<>();
        Customer customer1 = new Customer("aa",12);
        Customer customer2 = new Customer("aa",11);
        set.add(customer1);
        set.add(customer2);
        Iterator<Customer> iterator =set.iterator();
        while(iterator.hasNext()){
            Customer customer = iterator.next();
            System.out.println("name:" + customer.getName()+";age:" + customer.getAge());
        }
    }
}
package com.mall.controllor.alene;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

/**
 * Created by 60341 on 2020/3/23.
 */
public class CustomerComparator implements Comparator<Customer> {

    @Override
    public int compare(Customer o1, Customer o2) {
        if (o1.getName().compareTo(o2.getName())>0){
            return 1;
        }
        if (o1.getName().compareTo(o2.getName())<0){
            return -1;
        }
        return 0;
    }

    /*
    Set:底層二叉樹,TreeSet,排序:
    自然排序,讓對象所屬的類去實現Comparable接口,無參構造
    比較器接口Comparator,帶參構造
    Comparator 與 Comparable共存時,Comparator優先
 */
    public static void main(String[] args) {
        Set<Customer> set = new TreeSet<>(new CustomerComparator());
        Customer customer1 = new Customer("aa",12);
        Customer customer2 = new Customer("aa",11);
        Customer customer3 = new Customer("aa",2);
        set.add(customer1);
        set.add(customer2);
        set.add(customer3);
        Iterator<Customer> iterator = set.iterator();
        while(iterator.hasNext()){
            Customer customer = iterator.next();
            System.out.println("name:" + customer.getName()+";age:" + customer.getAge());
        }
    }
    /*
    輸出:
    name:aa;age:12
     */
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章