/import java.util.HashSet; import java.util.Objects; import java.util.Set; import java.util.TreeSet; public class Test1 { public static void main(String[] args){ Set<Test.Books> set = new HashSet<>(); TreeSet<Test.Books> tree = new TreeSet<>(); Test.Books books1 = new Test.Books(01,"坤坤成长史",520,"我的心"); Test.Books books2 = new Test.Books(02,"坤坤帅哥",1314,"我的脑子"); Test.Books books3 = new Test.Books(03,"双开门大冰箱",999,"我的幻想"); Test.Books books4 = new Test.Books(01,"坤坤成长史",520,"我的心"); set.add(books1); set.add(books2); set.add(books3); set.add(books4); //TreeSet(): 根据其元素的自然排序进行排序 tree.add(books1); tree.add(books2); tree.add(books3); tree.add(books4); System.out.println("HashSet:"); for(Test.Books s1 : set){ System.out.println(s1.getNumber() + s1.getname() + s1.getPrice() +s1.getPublisher()); } System.out.println("TreeSet:"); for(Test.Books s2 : tree){ System.out.println(s2.getNumber() + s2.getname() + s2.getPrice() +s2.getPublisher()); } } //Set没有带索引的方法 //TreeSet 无参构造方法 自然排序 让元素所属的类实现Comparable接口,重写compareTo方法 //重写方法,注意主要条件和次要条件 static abstract class Books implements Comparable<Books> { private int number; private String name; private double price; private String publisher; public Books(){ } Books(int number,String name,double price,String publisher){ super(); this.number = number; this.name = name; this.price = price; this.publisher = publisher; } public int getNumber(){ return number; } public String getname(){ return name; } public double getPrice() { return price; } public String getPublisher() { return publisher; } @Override public int hashCode() { return 0; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Books books = (Books) o; return number == books.number && Double.compare(books.price, price) == 0 && Objects.equals(name, books.name) && Objects.equals(publisher, books.publisher); } public int compareTo(Books s2){ return 1; } } }请帮我修改
时间: 2024-03-09 19:50:26 浏览: 104
java.util.Collection源码分析与深度理解
在 Test1 类中,你定义的 Books 类应该是一个具体的类而不是抽象类,因为你在 main 方法中实例化了它。此外,你在 compareTo 方法中返回了固定的值 1,这将导致 TreeSet 不会正确排序。你需要根据主要条件和次要条件来实现 compareTo 方法的逻辑。最后,你可以考虑在 Books 类中实现 hashCode 和 equals 方法,以确保在 Set 中正确处理对象。
下面是修改后的代码:
```
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
public class Test1 {
public static void main(String[] args) {
Set<Books> set = new HashSet<>();
TreeSet<Books> tree = new TreeSet<>();
Books books1 = new Books(01, "坤坤成长史", 520, "我的心");
Books books2 = new Books(02, "坤坤帅哥", 1314, "我的脑子");
Books books3 = new Books(03, "双开门大冰箱", 999, "我的幻想");
Books books4 = new Books(01, "坤坤成长史", 520, "我的心");
set.add(books1);
set.add(books2);
set.add(books3);
set.add(books4);
tree.add(books1);
tree.add(books2);
tree.add(books3);
tree.add(books4);
System.out.println("HashSet:");
for(Books s1 : set){
System.out.println(s1.getNumber() + s1.getName() + s1.getPrice() + s1.getPublisher());
}
System.out.println("TreeSet:");
for(Books s2 : tree){
System.out.println(s2.getNumber() + s2.getName() + s2.getPrice() + s2.getPublisher());
}
}
static class Books implements Comparable<Books> {
private int number;
private String name;
private double price;
private String publisher;
public Books(int number, String name, double price, String publisher){
this.number = number;
this.name = name;
this.price = price;
this.publisher = publisher;
}
public int getNumber() {
return number;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public String getPublisher() {
return publisher;
}
@Override
public int compareTo(Books o) {
if (this.price != o.price) {
return Double.compare(this.price, o.price);
} else if (!this.name.equals(o.name)) {
return this.name.compareTo(o.name);
} else {
return Integer.compare(this.number, o.number);
}
}
@Override
public int hashCode() {
return Objects.hash(number, name, price, publisher);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Books books = (Books) o;
return number == books.number &&
Double.compare(books.price, price) == 0 &&
Objects.equals(name, books.name) &&
Objects.equals(publisher, books.publisher);
}
}
}
```
阅读全文