Comparator定制排序

Comparator:定制排序
        ①声明一个类实现Comparator接口
        ②实现接口中的抽象方法compare(Object o1,Object o2)
        ③将该实现类的实例作为参数传递给TreeSet的构造器

 

package collection;

public class Person /*implements Comparable*/{
    private String name;
    private Integer age;

    public Person() {
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
/*
    @Override
    public int compareTo(Object o) {
        if (o instanceof Person){
            Person p=(Person) o;
            if (this.getAge().equals(p.getAge())){
                return this.getName().compareTo(p.getName());
            }
            return -this.getAge().compareTo(p.getAge());
        }
        return 0;
    }*/
}
package collection;

import java.util.Comparator;

public class MyComparator implements Comparator {
    @Override
    public int compare(Object o1, Object o2) {
        if (o1 instanceof Person && o2 instanceof Person){
            Person p1=(Person) o1;
            Person p2=(Person) o2;
            if (p1.getAge().equals(p2.getAge())){
                return p1.getName().compareTo(p2.getName());
            }
            return p1.getAge().compareTo(p2.getAge());
        }
        return 0;
    }
}
package collection;

import org.junit.Test;

import java.util.Comparator;
import java.util.HashSet;
import java.util.TreeSet;

/*
集合框架:
java.util.Collection:集合层次的根接口
                /--java.util.List:有序的,允许重复的;List系列集合都具有索引值
                    /--ArrayList:采用数组结构存储元素;(多用在查询操作多时,增删效率低,查询效率高)
                    /--LinkedList:采用链表结构存储元素;(多用于增删操作多时,增删效率高)
                    /--Vector:
                /--java.util.Set:无序的,不允许重复的;
                        /--HashSet:是Set 接口的典型实现类;判断元素是否存在的依据是,先比较hashCode(),若hashCode不存在,则直接存储
                                    若hashCode存在,则再通过equals()比较两个对象的内容
                                    注意:重写HashCode()和equals()方法时,两者要保持一致!
                            /--LinkedHashSet:是HashSet的子类,相似于HashSet多了链表维护元素的顺序,遍历效率高于HashSet
                                                增删效率低于HashSet
                        /--TreeSet:拥有指定的排序方式
                            >Comparable:自然排序
                                ①需要添加到TreeSet 集合中对象的类,实现Comparable接口
                                ②实现接口中的抽象方法compareTo(Object o)
                            >Comparator:定制排序
                                    ①声明一个类实现Comparator接口
                                    ②实现接口中的抽象方法compare(Object o1,Object o2)
                                    ③将该实现类的实例作为参数传递给TreeSet的构造器 */
public class SetTest {
        System.out.println(ts);
    }
    @Test
    public void test3(){
        Comparator com=new MyComparator();
        TreeSet ts=new TreeSet(com);

        ts.add(new Person("张三",15));
        ts.add(new Person("王五",22));
        ts.add(new Person("李四",33));
        ts.add(new Person("赵六",21));
        ts.add(new Person("赵六666",21));
        ts.add(new Person("赵六",21));
        System.out.println(ts);


    }
}

 

package collection;

import org.junit.Test;

import java.util.Comparator;
import java.util.HashSet;
import java.util.TreeSet;

/*
集合框架:
java.util.Collection:集合层次的根接口
                /--java.util.List:有序的,允许重复的;List系列集合都具有索引值
                    /--ArrayList:采用数组结构存储元素;(多用在查询操作多时,增删效率低,查询效率高)
                    /--LinkedList:采用链表结构存储元素;(多用于增删操作多时,增删效率高)
                    /--Vector:
                /--java.util.Set:无序的,不允许重复的;
                        /--HashSet:是Set 接口的典型实现类;判断元素是否存在的依据是,先比较hashCode(),若hashCode不存在,则直接存储
                                    若hashCode存在,则再通过equals()比较两个对象的内容
                                    注意:重写HashCode()和equals()方法时,两者要保持一致!
                            /--LinkedHashSet:是HashSet的子类,相似于HashSet多了链表维护元素的顺序,遍历效率高于HashSet
                                                增删效率低于HashSet
                        /--TreeSet:拥有指定的排序方式
                            >Comparable:自然排序
                                ①需要添加到TreeSet 集合中对象的类,实现Comparable接口
                                ②实现接口中的抽象方法compareTo(Object o)
                            >Comparator:定制排序
                                    ①声明一个类实现Comparator接口
                                    ②实现接口中的抽象方法compare(Object o1,Object o2)
                                    ③将该实现类的实例作为参数传递给TreeSet的构造器
 */
public class SetTest {
   
    @Test
    public void test3(){
        Comparator com=new MyComparator();
        TreeSet ts=new TreeSet(com);

        ts.add(new Person("张三",15));
        ts.add(new Person("王五",22));
        ts.add(new Person("李四",33));
        ts.add(new Person("赵六",21));
        ts.add(new Person("赵六666",21));
        ts.add(new Person("赵六",21));
        System.out.println(ts);
    }
    @Test
    public void test4(){
        //匿名内部类实现接口
        Comparator com=new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Person && o2 instanceof Person){
                    Person p1=(Person) o1;
                    Person p2=(Person) o2;
                    if (p1.getAge().equals(p2.getAge())){
                        return p1.getName().compareTo(p2.getName());
                    }
                    return p1.getAge().compareTo(p2.getAge());
                }
                return 0;
            }
        };

        TreeSet ts=new TreeSet(com);

        ts.add(new Person("张三",15));
        ts.add(new Person("王五",22));
        ts.add(new Person("李四",33));
        ts.add(new Person("赵六",21));
        ts.add(new Person("赵六666",21));
        ts.add(new Person("赵六",21));
        System.out.println(ts);

    }
    @Test
    public void test5(){
        //匿名内部类+匿名对象
        TreeSet ts=new TreeSet(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Person && o2 instanceof Person){
                    Person p1=(Person) o1;
                    Person p2=(Person) o2;
                    if (p1.getAge().equals(p2.getAge())){
                        return p1.getName().compareTo(p2.getName());
                    }
                    return p1.getAge().compareTo(p2.getAge());
                }
                return 0;
            }
        });

        ts.add(new Person("张三",15));
        ts.add(new Person("王五",22));
        ts.add(new Person("李四",33));
        ts.add(new Person("赵六",21));
        ts.add(new Person("赵六666",21));
        ts.add(new Person("赵六",21));
        System.out.println(ts);

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值