比较器的用法(有疑问)

比较器

public interface Comparable<T>

此接口强行对实现它的每个类的对象进行整体排序。此排序被称为该类的自然排序,类的 compareTo 方法被称为它的自然比较方法

实现此接口的对象列表(和数组)可以通过 Collections.sort(和 Arrays.sort)进行自动排序。实现此接口的对象可以用作有序映射表中的键或有序集合中的元素,无需指定比较器

例子,实现了的Comparable

import java.util.Arrays;

 

class MyInteger implements Comparable {

    public int n;

 

    public MyInteger(int n) {

       this.n = n;

    }

 

    public int compareTo(Object o) {

       if (o instanceof MyInteger) {

           MyInteger mi = (MyInteger) o;

           if (mi.n == this.n) {

              return 0;

           } else if (mi.n > this.n) {

              return 1;

           } else {

              return -1;

           }

       } else {

           return -1;

       }

    }

}

 

public class test6 {

    public static void main(String[] args) {

       int n = 10;

       MyInteger[] mis = new MyInteger[n];

       // 赋值

       for (int i = 0; i < mis.length; i++) {

           mis[i] = new MyInteger((int) (Math.random() * 100));

       }

       // 将赋值的结果输出来。

       for (MyInteger m : mis) {

           System.out.print(m.n + "\t");

       }

      //Arrays根据元素的自然顺序,对指定对象数组按升序进行排序。(静态方法)

       Arrays.sort(mis);

       System.out.println();

       // 将排序后的结果输出来。

       for (MyInteger m : mis) {

           System.out.print(m.n + "\t");

       }

    }

}

 

 

public interface Comparator<T>

比较函数强行对某些对象 collection 进行整体排序。可以将 Comparator 传递给 sort 方法(如 Collections.sort),从而允许在排序顺序上实现精确控制。还可以使用 Comparator 来控制某些数据结构(如 TreeSet TreeMap)的顺序。

这样实现对吗啊,因为这个接口是针对某些对象 collection,那么实现该接口的是某些对象 collection,感觉自己实现对了问题。

 

import java.util.*;

 

class Student implements  Comparator  {

    int num;

    String name;

 

    Student() {

       this.name = null;

       this.num = 0;

    }

 

    Student(int num, String name) {

       this.name = name;

       this.num = num;

    }

   

    // 保证相等的对象或得的哈希吗是相同的。

    public int HashCode()

    {

       return num * name.hashCode();

    }

 

    public boolean equals(Object o) {

       Student s = (Student) o;

       return num == s.num && name.equals(s.name);

    }

   

    // 实现升序序排序

    //如何将两个接口同时在treeset类中实现和运行呢

    public int compareTo(Object o)

    {

       Student s = (Student) o;

       return num > s.num ? 1 : (num == s.num ? 0 : -1);

 

    }

 

    public String toString() {

       return num + ":" + name;

    }

 

    public int compare(Object o1, Object o2) // 实现降序排序

    {

       Student s1 = (Student) o1;

       Student s2 = (Student) o2;

       int result = s1.num > s2.num ? 1 : (s1.num == s2.num ? 0 : -1);

       if (result == 0) { // String类实现了compareTo()方法.

           result = s1.name.compareTo(s2.name);

       }

       return result;

    }

 

    public static void printElements(Collection c) {

          Iterator it = c.iterator();

       while (it.hasNext()) {

           System.out.println(it.next());

       }

    }

 

}

 

public class test3{

       // 不了解comparable接口可以用在哪里,这些接口的关系不是很清楚。

       // 如果自定义类对象要加入TreeSet要实现Comparable接口

    public static void main(String[] args) {

       Comparator d = new Student();

       TreeSet ts = new TreeSet(d);

 // /TreeSet td=new TreeSet(d);treeset 无法使用Comparable接口

 

       ts.add(new Student(2, "lisi"));

       ts.add(new Student(1, "wangwu"));

       ts.add(new Student(3, "zhangsan"));

       ts.add(new Student(3, "zhangsan"));

       System.out.println("降序为:");

       Student.printElements(ts);

// Collections.sz这样写这个方法实现第一题,是有问题,他说一个实例既可以升又可以降

       Comparator h = Collections.reverseOrder(d);

       TreeSet td = new TreeSet(h);

       td.add(new Student(2, "lisi"));

       td.add(new Student(1, "wangwu"));

       td.add(new Student(3, "zhangsan"));

       td.add(new Student(3, "mybole"));

        System.out.println("升序为:");

       Student.printElements(td);

      

 

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值