通过Comparable来实现对自身的比较

时间:2019-03-25
本文章向大家介绍通过Comparable来实现对自身的比较,主要包括通过Comparable来实现对自身的比较使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
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();
 }
}