import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
 
/**
 * The Class Book.
 */
public class Book implements Comparable<book> {
 
 /** The id. */
 private long id;
 
 /** The name. */
 private String name;
 
 /**
  * Instantiates a new book.
  */
 public Book() {
 }
 
 /**
  * Gets the id.
  *
  * @return the id
  */
 public long getId() {
  return id;
 }
 
 /**
  * Sets the id.
  *
  * @param id
  *            the new id
  */
 public void setId(long id) {
  this.id = id;
 }
 
 /**
  * Gets the name.
  *
  * @return the name
  */
 public String getName() {
  return name;
 }
 
 /**
  * Sets the name.
  *
  * @param name
  *            the new name
  */
 public void setName(String name) {
  this.name = name;
 }
 
 /*
  * (non-Javadoc)
  *
  * @see java.lang.Comparable#compareTo(java.lang.Object)
  */
 public int compareTo(Book o) {
  return new CompareToBuilder().append(this.getId(), o.getId()).toComparison();
 }
}

通过Comparable来实现对自身的比较的更多相关文章

  1. Java中Comparable与Comparator的区别

    相同 Comparable和Comparator都是用来实现对象的比较.排序 要想对象比较.排序,都需要实现Comparable或Comparator接口 Comparable和Comparator都 ...

  2. 12.Java中Comparable接口,Readable接口和Iterable接口

    1.Comparable接口 说明:可比较(可排序的) 例子:按照MyClass的y属性进行生序排序 class MyClass implements Comparable<MyClass> ...

  3. 泛型的排序问题(Collections.sort及Comparable的应用)

    一.前言    java中对泛型(集合类型)排序的问题,主要采用了两张方式一种是对要排序的实体类,实现Comparable接口,另一种方式,Collections集合工具类进行排序. 二.实现Comp ...

  4. 【集合框架】JDK1.8源码分析之Comparable && Comparator(九)

    一.前言 在Java集合框架里面,各种集合的操作很大程度上都离不开Comparable和Comparator,虽然它们与集合没有显示的关系,但是它们只有在集合里面的时候才能发挥最大的威力.下面是开始我 ...

  5. 对象比较器:Comparable和Comparator

    在进行对象数组排序的过程中需要使用到比较器,比较器有两个:Comparable和Comparator ①.java.lang.Comparable:是在类定义是时候默认实现好的接口,里面提供有一个co ...

  6. java中的Comparable接口

    类对象之间比较"大小"往往是很有用的操作,比如让对象数组排序时,就需要依赖比较操作.对于不同的类有不同的语义.如Student类,比较2个学生对象可以比较他们的score分数来评判 ...

  7. java://Comparator、Comparable的用法(按照要求将map集合的键值对进行顺序输出)

    import java.util.*; public class Person implements Comparable<Person>//使Person的属性具有比较性 { priva ...

  8. Java中Comparable和Comparator接口区别分析

    Java中Comparable和Comparator接口区别分析 来源:码农网 | 时间:2015-03-16 10:25:20 | 阅读数:8902 [导读] 本文要来详细分析一下Java中Comp ...

  9. Comparable和Comparator的区别

    Comparable Comparable可以认为是一个内比较器,实现了Comparable接口的类有一个特点,就是这些类是可以和自己比较的,至于具体和另一个实现了Comparable接口的类如何比较 ...

  10. java.lang.Comparable<T> 接口

    package java.lang; import java.util.*; public interface Comparable<T> { public int compareTo(T ...

随机推荐

  1. Editor HDU - 4699 (栈)

    Problem Description   Sample Input 8 I 2 I -1 I 1 Q 3 L D R Q 2   Sample Output 2 3 Hint The followi ...

  2. Shell学习之结合正则表达式与通配符的使用(五)

    Shell学习之结合正则表达式与通配符的使用 目录 通配符 正则表达式与通配符 通配符 通配符的使用 正则表达式 正则表达式 正则表达式的使用 通配符 正则表达式与通配符 正则表达式用来在文件中匹配符 ...

  3. Nginx配置以及域名转发

    工程中的nginx配置 #user nobody; worker_processes 24; error_log /home/xxx/opt/nginx/logs/error.log; pid /ho ...

  4. shell delete with line number

    If you want to delete lines 5 through 10 and 12: sed -e '5,10d;12d' file This will print the results ...

  5. hadoop安装过程中出现的错误

    此次来记录一下我在安装Hadoop安装过程中出现的错误,安装过程参照慕课网林子雨教程进行安装,在尝试过程中出现的错误如下: 1.在安装Ubuntu时,新建虚拟电脑时,并没有在版本的输入框中有Ubunt ...

  6. HDU.4694.Important Sisters(支配树)

    HDU \(Description\) 给定一张简单有向图,起点为\(n\).对每个点求其支配点的编号和. \(n\leq 50000\). \(Solution\) 支配树. 还是有点小懵逼. 不管 ...

  7. 将分支推送到远程存储库时遇到错误: rejected Updates were rejected because the remote contains work that you do not have locally

    在仓库目录下执行 git pull origin master --allow-unrelated-histories 之后就可以成功的pull,push了

  8. Flask 三方组件 WTForms

    WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证 from flask import Blueprint from flask import request fro ...

  9. linux使用Anaconda管理多个版本的Python环境

    1.下载(直接到清华镜像下载) 下载链接,选择合适的版本,我试了几个,选择了一个下载最快的,原谅我的渣渣网速, 官网在国外,必须找镜像,不然很慢很慢,,,,,, 此步骤真的很慢,重新选择下载网址,这个 ...

  10. Spring 4 MVC example with Maven - [Source Code Download]

    In this tutorial, we show you a Spring 4 MVC example, using Maven build tool. Technologies used : Sp ...