日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

treeSet中对象的比较

發(fā)布時間:2025/3/17 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 treeSet中对象的比较 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

TreeSet要實現(xiàn)對象的排序,該對象要實現(xiàn)Comparable 接口compareTO方法

public class Book implements Comparable {

  public int id;
?? ?public String name;
?? ?public double price;
?? ?public String press;

? @Override
?? ?public int compareTo(Object o) {
?? ??? ?Book book=(Book)o;
?? ??? ?int val=this.id-book.id;?????? 先通過id比較??? id是數(shù)字可以直接做差
?? ??? ?if(val==0) {???????????????????????? 如果id相同然后比較名字
?? ??? ??? ?val=this.name.compareTo(book.name);??
?? ??? ?}
?? ??? ?return val;
?? ?}

public int getId() {
?? ??? ?return id;
?? ?}
?? ?public void setId(int id) {
?? ??? ?this.id = id;
?? ?}
?? ?public String getName() {
?? ??? ?return name;
?? ?}
?? ?public void setName(String name) {
?? ??? ?this.name = name;
?? ?}
?? ?public double getPrice() {
?? ??? ?return price;
?? ?}
?? ?public void setPrice(double price) {
?? ??? ?this.price = price;
?? ?}
?? ?public String getPress() {
?? ??? ?return press;
?? ?}
?? ?public void setPress(String press) {
?? ??? ?this.press = press;
?? ?}
?? ?public Book(int id, String name, double price, String press) {
?? ??? ?super();
?? ??? ?this.id = id;
?? ??? ?this.name = name;
?? ??? ?this.price = price;
?? ??? ?this.press = press;
?? ?}
?? ?@Override
?? ?public String toString() {
?? ??? ?return "Book [id=" + id + ", name=" + name + ", price=" + price + ", press=" + press + "]";
?? ?}
?? ?public Book() {
?? ??? ?super();
?? ??? ?// TODO Auto-generated constructor stub
?? ?}
?? ?@Override
?? ?public int hashCode() {
?? ??? ?final int prime = 31;
?? ??? ?int result = 1;
?? ??? ?result = prime * result + id;
?? ??? ?result = prime * result + ((name == null) ? 0 : name.hashCode());
?? ??? ?result = prime * result + ((press == null) ? 0 : press.hashCode());
?? ??? ?long temp;
?? ??? ?temp = Double.doubleToLongBits(price);
?? ??? ?result = prime * result + (int) (temp ^ (temp >>> 32));
?? ??? ?return result;
?? ?}
?? ?@Override
?? ?public boolean equals(Object obj) {
?? ??? ?if (this == obj)
?? ??? ??? ?return true;
?? ??? ?if (obj == null)
?? ??? ??? ?return false;
?? ??? ?if (getClass() != obj.getClass())
?? ??? ??? ?return false;
?? ??? ?Book other = (Book) obj;
?? ??? ?if (id != other.id)
?? ??? ??? ?return false;
?? ??? ?if (name == null) {
?? ??? ??? ?if (other.name != null)
?? ??? ??? ??? ?return false;
?? ??? ?} else if (!name.equals(other.name))
?? ??? ??? ?return false;
?? ??? ?if (press == null) {
?? ??? ??? ?if (other.press != null)
?? ??? ??? ??? ?return false;
?? ??? ?} else if (!press.equals(other.press))
?? ??? ??? ?return false;
?? ??? ?if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
?? ??? ??? ?return false;
?? ??? ?return true;
?? ?}
?? ?public void setBook(Book book) {
?? ??? ?this.book=book;
?? ??? ?
?? ?}
?? ?public int getBook() {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?return 0;
?? ?}
?? ?
?
???

?? ?
???

轉(zhuǎn)載于:https://www.cnblogs.com/FuckJava/p/9075356.html

總結(jié)

以上是生活随笔為你收集整理的treeSet中对象的比较的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。