博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java——TreeSet中Comparable/Comparator比较器
阅读量:5101 次
发布时间:2019-06-13

本文共 4847 字,大约阅读时间需要 16 分钟。

一、TreeSet中比较器:

  1)Comparable:实体类实现Comparable接口,重写compareTo方法;

  2)Comparator:写一个实现类实现Comparator接口,重写compare方法;创建TreeSet时候传入该实体类的对象;

  3)匿名实现Comparator;

  4)lambdo实现Comparator;

二、Comparable:

  1、实体类:

 

public class Person implements Comparable {    private String name;    private int age;    Person(String name,int age){        this.name=name;        this.age=age;    }    public String getName(){        return name;    }    public int getAge(){        return age;    }    //实现Comparable接口,重写compareTo方法;    public int compareTo(Object obj){        if (!(obj instanceof Person)){            throw new RuntimeException("不是正确对象");        }        Person p=(Person)obj;        if (p.age>this.age)return -1;        if (p.age==this.age){            return this.name.compareTo(p.name);        };        return 1;    }}

 

  2、测试类:

public class Test {    public static void main(String[] args) {        TreeSet ts = new TreeSet();        ts.add(new Person("zs0", 20));        ts.add(new Person("zs1", 20));        ts.add(new Person("zs1", 21));        ts.add(new Person("zs9", 19));        for (Iterator
it = ts.iterator(); it.hasNext(); ) { Person p = it.next(); System.out.println(p.getName()+"..."+p.getAge()); } }}

三、Comparator:

  1、实体类:

public class Student {    private String name;    private int age;    public Student(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 String toString() {        return "Student{" +                "name='" + name + '\'' +                ", age=" + age +                '}';    }}

  2、comparator实现类:

 

public class ComparatorImpl implements Comparator {    @Override    public int compare(Object o1, Object o2) {        if (o1==o2)return 0;        if (!(o1 instanceof Student) || !(o2 instanceof Student)){            throw new RuntimeException("类型不对");        }        Student s1=(Student)o1;        Student s2=(Student)o2;        int num=s1.getName().compareTo(s2.getName());        if (num==0){            return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));        }        return num;    }}

  3、测试类:

public class Test {    public static void main(String[] args) {        TreeSet ts=new TreeSet(new ComparatorImpl());        ts.add(new Student("z1", 1));        ts.add(new Student("z3", 3));        ts.add(new Student("z2", 2));        ts.add(new Student("z3", 2));        ts.add(new Student("z3", 2));        Iterator it=ts.iterator();        while (it.hasNext()){            System.out.println(it.next().toString());        }    }}

四、comparator匿名实现:

 

public class Test2 {    public static void main(String[] args) {        TreeSet ts = new TreeSet(new Comparator() {            @Override            public int compare(Object o1, Object o2) {                if (o1 == o2) return 0;                if (!(o1 instanceof Student) || !(o2 instanceof Student)) {                    throw new RuntimeException("类型不对");                }                Student s1 = (Student) o1;                Student s2 = (Student) o2;                int num = s1.getName().compareTo(s2.getName());                if (num == 0) {                    return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));                }                return num;            }        });        ts.add(new Student("z1", 1));        ts.add(new Student("z3", 3));        ts.add(new Student("z2", 2));        ts.add(new Student("z3", 2));        ts.add(new Student("z3", 2));        Iterator it = ts.iterator();        while (it.hasNext()) {            System.out.println(it.next().toString());        }    }}

五、lambda实现comparator:

  //lambda需要jdk1.8;

public class Test3 {    public static void main(String[] args) {        TreeSet ts = new TreeSet((o1, o2)->{            if (o1 == o2) return 0;            if (!(o1 instanceof Student) || !(o2 instanceof Student)) {                throw new RuntimeException("类型不对");            }            Student s1 = (Student) o1;            Student s2 = (Student) o2;            int num = s1.getName().compareTo(s2.getName());            if (num == 0) {                return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));            }            return num;        });        ts.add(new Student("z1", 1));        ts.add(new Student("z3", 3));        ts.add(new Student("z2", 2));        ts.add(new Student("z3", 2));        ts.add(new Student("z3", 2));        Iterator it = ts.iterator();        while (it.hasNext()) {            System.out.println(it.next().toString());        }    }}

 

转载于:https://www.cnblogs.com/Tractors/p/11241583.html

你可能感兴趣的文章
Eclipse 反编译之 JadClipse
查看>>
Python入门-函数
查看>>
[HDU5727]Necklace(二分图最大匹配,枚举)
查看>>
距离公式汇总以及Python实现
查看>>
设计模式之装饰者模式
查看>>
一道不知道哪里来的容斥题
查看>>
Blender Python UV 学习
查看>>
window添加右键菜单
查看>>
入手腾龙SP AF90mm MACRO
查看>>
python学习4 常用内置模块
查看>>
Window7上搭建symfony开发环境(PEAR)
查看>>
ResolveUrl的用法
查看>>
Linux内核态、用户态简介与IntelCPU特权级别--Ring0-3
查看>>
第23月第24天 git命令 .git-credentials git rm --cached git stash clear
查看>>
java SE :标准输入/输出
查看>>
一些方便系统诊断的bash函数
查看>>
【转载】基于vw等viewport视区相对单位的响应式排版和布局
查看>>
<转>关于MFC的多线程类 CSemaphore,CMutex,CCriticalSection,CEvent
查看>>
jquery中ajax返回值无法传递到上层函数
查看>>
css3之transform-origin
查看>>