集合,去重!

 

/*
* 如果一个类的元素要想进行自然排序,就必须实现自然排序接口
* */
public class Book implements Comparable {
    private int id;
    private String name;
    private String author;
    private int price;

    public Book() {
    }

    public Book(int id, String name, String author, int price) {
        this.id = id;
        this.name = name;
        this.author = author;
        this.price = price;
    }

    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 String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Book book = (Book) o;

        if (id != book.id) return false;
        if (Double.compare(book.price, price) != 0) return false;
        if (name != null ? !name.equals(book.name) : book.name != null) return false;
        return author != null ? author.equals(book.author) : book.author == null;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (author != null ? author.hashCode() : 0);
        temp = Double.doubleToLongBits(price);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    @Override
    //写自己的比较规则
    public int compareTo(Object o) {
        //这里返回什么,根据比较结果而定(根据排序规则而定)
        //return 0;
        Book b = (Book)o;
        //主要条件  价格
        int i = this.price-b.price;
        //次要条件
        int ii = i==0?this.id-b.id:i;
        int iii = ii==0?this.name.compareTo(b.name):ii;
        int iiii = iii==0?this.author.compareTo(b.author):iii;
        return iiii;
    }
}
import java.util.LinkedHashSet;

//LinkedHashSet  存储自定义对象遍历
public class Demo4 {
    public static void main(String[] args) {
        LinkedHashSet<Book> ls = new LinkedHashSet<>();
        ls.add(new Book(4,"西游记","吴承恩",67));
        ls.add(new Book(1,"水浒传","施耐庵",88));
        ls.add(new Book(2,"三国演绎","罗贯中",77));
        ls.add(new Book(3,"红楼梦","曹雪芹",66));
        ls.add(new Book(2,"三国演绎","罗贯中",77));
        ls.add(new Book(3,"红楼梦","曹雪芹",66));

        for (Object l : ls) {
            System.out.println(l);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值