欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java内部比较器和外部比较器

程序员文章站 2024-01-13 23:35:52
...

外部比较器,实现comparator,重写compare,以下例子实现包含对象的集合的排序。排序时调用的函数是collections.sort(list,比较器)。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author :Handoking
 * @date : 2019/5/24
 */
//外部比较器
class MyCompare implements Comparator<Students> {
    @Override
    public int compare(Students s1,Students s2){
        if (s1.getAge()>s2.getAge()){
            return 1;
        }else if (s1.getAge()==s2.getAge()){
            return 0;
        }else{
            return -1;
        }
    }
}
class Students{
    private int age;
    private String name;

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

    public Students() {
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString(){
    	return "Name:"+name+",Age:"+age;
    }
}
public class TestCompare{
    public static void main(String[] args){
        List<Students> arr = new ArrayList<>();
        arr.add(new Students(20,"mark"));
        arr.add(new Students(22,"kaven"));
        arr.add(new Students(16,"daxia"));
        arr.add(new Students(25,"handoking"));
        Collections.sort(arr,new MyCompare());
        System.out.println(arr.get(1).toStirng);
        }
}

内部比较器就是把比较器定义在内部,这是要实现的接口为comparable,重写compareTo,实现包含实体类对象的集合的排序时,调用的是collections.sort(list).

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author :Handoking
 * @date : 2019/5/24
 */
//内部比较器
class Students implements Comparable<Students> {
    private int age;
    private String name;

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

    public Students() {
    }

    public int getAge() {
        return age;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }
     @Override
     public int compareTo(Students s1){
         if (this.getAge()>s1.getAge()){
             return 1;
         }else if (this.getAge()==s1.getAge()){
             return 0;
         }else{
             return -1;
         }
     }
    @Override
    public String toString(){
        return "Name:" + name +",Age:"+age;
    }
}
public class TestCompare{
    public static void main(String[] args){
        List<Students> arr = new ArrayList<>();
        arr.add(new Students(20,"mark"));
        arr.add(new Students(22,"kaven"));
        arr.add(new Students(16,"daxia"));
        arr.add(new Students(25,"handoking"));
        Collections.sort(arr);
        System.out.println(arr.get(0).toString());
        }
}
相关标签: 比较器