当前位置: 首页 > news >正文

Comparable与Comparator的区别

转自:http://blog.csdn.net/wgw335363240/archive/2010/05/15/5595084.aspx

1.Comparable是对象内部需要实现的接口,如果一个对象要想用利用Array.sort进行排序,那么这个对象必须实现Comparable的接口,比较的实现是在对象的内部进行的。

2.Comparator接口是在对象外实现的接口,主要是方便了对没有实现Comparable接口的对象的比较和排序,比如:当我们对已经有的一个类进行排序的时候,这个类又没有实现Comparable接口,那么我们可以利用Collections.sort来排序,同时实现一个Comparator接口接口实现对这个类的排序。写了个测试代码,如下,MyInteger是一个持久化对象,由于MyInteger实现了Comparable接口,所以可以用Arrays.sort来排序,当然也可以通过Collections.sort来排序。

一、持久化对象,MyInteger

/**
 * 
 */
package com.test.compare;

/**
 * 需要排序的持久化类
 * 
 * @author rey
 * 
 */
public class MyInteger implements Comparable {

 private int m_nIngeger = 0;

 /**
  * 构造函数
  */
 public MyInteger(int _nInteger) {
  super();
  this.setIngeger(_nInteger);
 }

 /**
  * @return the m_nIngeger
  */
 public int getIngeger() {
  return m_nIngeger;
 }

 /**
  * @param mNIngeger
  *            the m_nIngeger to set
  */
 public void setIngeger(int _nInteger) {
  m_nIngeger = _nInteger;
 }

 @Override
 public int compareTo(Object _oObject) {
  if (_oObject == null || !(_oObject instanceof MyInteger)) {
   return -1;
  }

  MyInteger anotherMyInteger = (MyInteger) _oObject;
  int nAnother = anotherMyInteger.getIngeger();
  if (this.getIngeger() < nAnother) {
   return -1;
  } else if (this.getIngeger() == nAnother) {
   return 0;
  }

  return 1;
 }

}

二、测试数据类

package com.test.compare;

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

public class TestMain {

 /**
  * @param args
  */
 public static void main(String[] args) {
  List _sortList = new ArrayList();
  _sortList.add(new MyInteger(1));
  _sortList.add(new MyInteger(10));
  _sortList.add(new MyInteger(15));
  _sortList.add(new MyInteger(3));
  _sortList.add(new MyInteger(78));
  _sortList.add(new MyInteger(45));
  _sortList.add(new MyInteger(2));
  _sortList.add(new MyInteger(56));
  _sortList.add(new MyInteger(35));

  // 用Collections.sort来排序
  sortByCollection(_sortList);

  // 打印信息
  System.out.println("Collections.sort的排序结果:");
  for (int i = 0; i < _sortList.size(); i++) {
   MyInteger aMyInteger = (MyInteger) _sortList.get(i);
   if (aMyInteger == null) {
    continue;
   }
   System.out.println(aMyInteger.getIngeger());
  }

  // 重新构造list对象
  _sortList = new ArrayList();
  _sortList.add(new MyInteger(1));
  _sortList.add(new MyInteger(10));
  _sortList.add(new MyInteger(15));
  _sortList.add(new MyInteger(3));
  _sortList.add(new MyInteger(78));
  _sortList.add(new MyInteger(45));
  _sortList.add(new MyInteger(2));
  _sortList.add(new MyInteger(56));
  _sortList.add(new MyInteger(35));
  
  // 用Arrays.sort来排序
  Object[] sortArrays = _sortList.toArray();
  Arrays.sort(sortArrays);

  // 打印信息
  System.out.println("Arrays.sort的排序结果:");
  for (int i = 0; i < sortArrays.length; i++) {
   MyInteger aMyInteger = (MyInteger) sortArrays[i];
   if (aMyInteger == null) {
    continue;
   }
   System.out.println(aMyInteger.getIngeger());
  }

 }

 /**
  * 从Colections.sort来排序
  * 
  * @param _sortList
  */
 private static void sortByCollection(List _sortList) {
  if (_sortList == null) {
   return;
  }

  Collections.sort(_sortList, new Comparator() {

   @Override
   public int compare(Object o1, Object o2) {
    MyInteger oneInteger = (MyInteger) o1;
    MyInteger anotherInteger = (MyInteger) o2;
    if (oneInteger.getIngeger() < anotherInteger.getIngeger()) {
     return -1;
    } else if (oneInteger.getIngeger() == anotherInteger
      .getIngeger()) {
     return 0;
    }
    return 1;
   }

  });
 }
}

相关文章:

  • java异常分类
  • [51nod1610]路径计数
  • Effective Java 笔记(一)
  • Linux 下配置 Python IDE——Emacs
  • java面试题总结
  • mybatis中写sql语句时需要转义的字符
  • apache的作用和tomcat的区别
  • 微信公众号token验证失败的一些总结
  • java 静态导入
  • 【bzoj3673】可持久化并查集 by zky
  • HashSet序列化问题
  • QT学习之路--菜单、工具条、状态栏
  • 序列化-理解readResolve()
  • Java thread的Interrupt, isInterrupt, interrupted
  • Java字符串
  • 2017年终总结、随想
  • Cookie 在前端中的实践
  • JavaScript服务器推送技术之 WebSocket
  • JS字符串转数字方法总结
  • Redux 中间件分析
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • SQLServer之创建显式事务
  • Theano - 导数
  • use Google search engine
  • vue-router 实现分析
  • 阿里云ubuntu14.04 Nginx反向代理Nodejs
  • 纯 javascript 半自动式下滑一定高度,导航栏固定
  • 大快搜索数据爬虫技术实例安装教学篇
  • 类orAPI - 收藏集 - 掘金
  • 面试题:给你个id,去拿到name,多叉树遍历
  • 使用 Docker 部署 Spring Boot项目
  • 学习HTTP相关知识笔记
  • 云大使推广中的常见热门问题
  • Java数据解析之JSON
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • #ifdef 的技巧用法
  • #pragma data_seg 共享数据区(转)
  • #控制台大学课堂点名问题_课堂随机点名
  • #中国IT界的第一本漂流日记 传递IT正能量# 【分享得“IT漂友”勋章】
  • (免费领源码)Java#Springboot#mysql农产品销售管理系统47627-计算机毕业设计项目选题推荐
  • (七)c52学习之旅-中断
  • (数据结构)顺序表的定义
  • (转)JAVA中的堆栈
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • *2 echo、printf、mkdir命令的应用
  • .NET 回调、接口回调、 委托
  • .net分布式压力测试工具(Beetle.DT)
  • .NET值类型变量“活”在哪?
  • @DateTimeFormat 和 @JsonFormat 注解详解
  • [ C++ ] 继承
  • [120_移动开发Android]008_android开发之Pull操作xml文件
  • [BeginCTF]真龙之力
  • [BZOJ 3680]吊打XXX(模拟退火)
  • [BZOJ] 2427: [HAOI2010]软件安装
  • [C# 开发技巧]实现属于自己的截图工具