首页 > 编程语言 > 详细

java compare and comparator

时间:2019-04-04 19:42:30      阅读:119      评论:0      收藏:0      [点我收藏+]

1.Comparable简介

此接口对实现它的每个类的对象强加一个总排序。这种排序被称为类的自然排序,类的compareTo方法被称为其自然比较方法。可以通过

Collections.sort(和Arrays.sort)自动对实现此接口的对象的列表(和数组)进行排序。实现此接口的对象可用作有序映射中的键或有序

集中的元素,而无需指定比较器。

注:若一个类实现了该接口,说明该类本身是支持排序的。

This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class‘s natural ordering,

and the class‘s compareTo method is referred to as its natural comparison method. Lists (and arrays) of objects that implement this interface can

be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or

as elements in a sorted set, without the need to specify a comparator.

2.Comparable定义

实现Comparable接口仅需实现compareTo方法。

通过x.compareTo(y)来比较x与y的大小:1)返回负数,说明x小于y;2)返回0,说明x与y相等;3)返回正数,说明x大于y。

1 package java.lang;
2 import java.util.*;
3 
4 public interface Comparable<T> {
5     public int compareTo(T o);
6 }

3.Comparator简介

该接口内部是一个比较函数,它对某些对象集合施加总排序。可以将比较器传递给排序方法(例如Collections.sort或Arrays.sort),以便

精确控制排序顺序。比较器还可用于控制某些数据结构的顺序(例如有序集或有序映射),或者为不具有自然顺序的对象集合提供排序。

注:若一个类没有实现Comparable接口,则该类自身无法排序;此时可以使用Comparator帮助这个类进行排序。

A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as 

Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain

data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don‘t have a natural ordering.

4. Comparator定义

1 package java.util;
2 
3 public interface Comparator<T> {
4 
5     int compare(T o1, T o2);
6 
7     boolean equals(Object obj);
8 }

 5. 示例

5.1 Customer.java

该类实现了Comparable接口,即该类的成员对象自身是支持排序的。

 1 public class Customer implements Comparable<Customer>{
 2 
 3     private int customerId;
 4     private String customerName;
 5 
 6     public Customer(Integer customerId, String customerName) {
 7         this.customerId = customerId;
 8         this.customerName = customerName;
 9     }
10 
11     public int getCustomerId() {
12         return customerId;
13     }
14     public String getCustomerName() {
15         return customerName;
16     }
17 
18     @Override
19     public String toString() {
20         return "Customer [customerId=" + customerId + ", customerName=" + customerName + "]";
21     }
22 
23     //按id或name排序
24     @Override
25     public int compareTo(Customer o) {
26         //可以对整体添加负号决定升降序
27 //        return this.customerId - o.customerId;
28         return this.customerName.compareTo(o.customerName);
29 
30     }
31 }

5.2 CustomerComparator.java

该类实现了Comparator接口,帮助Customer对象按Id排序。

 1 import java.util.Comparator;
 2 
 3 public class CustomerComparator implements Comparator<Customer> {
 4 
 5     @Override
 6     public int compare(Customer c1, Customer c2) {
 7         // 按Id排序
 8         return c1.getCustomerId() - c2.getCustomerId();
 9      }
10 }

5.3 SortFunc.java

使用两种接口的排序方法对list进行排序。

 1 import java.util.*;
 2 
 3 public class SortFunc {
 4     public static void main(String[] args){
 5         List<Customer> list = new ArrayList<>();
 6         list.add(new Customer(1, "A"));
 7         list.add(new Customer(2, "C"));
 8         list.add(new Customer(3, "D"));
 9         list.add(new Customer(4, "B"));
10 
11         // 原始排序
12         System.out.println("原始的排序:"+list);
13 
14         // 使用compare接口按name排序
15         Collections.sort(list);
16         System.out.println("使用compare接口按名称排序:"+list);
17 
18         // 使用comparator接口按id排序
19 //        Collections.sort(list, new CustomerComparator());     // 两个方式
20         list.sort(new CustomerComparator());
21         System.out.println("使用comparator接口按id排序:"+list);
22     }
23 }

5.4 结果

1 原始的排序:[Customer [customerId=1, customerName=A], Customer [customerId=2, customerName=C], Customer [customerId=3, customerName=D], Customer [customerId=4, customerName=B]]
2 使用compare接口按名称排序:[Customer [customerId=1, customerName=A], Customer [customerId=4, customerName=B], Customer [customerId=2, customerName=C], Customer [customerId=3, customerName=D]]
3 使用comparator接口按id排序:[Customer [customerId=1, customerName=A], Customer [customerId=2, customerName=C], Customer [customerId=3, customerName=D], Customer [customerId=4, customerName=B]]

!!!

java compare and comparator

原文:https://www.cnblogs.com/jfl-xx/p/10656433.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!