java中hashset和tree set区别
时间: 2023-05-16 13:05:05 浏览: 73
HashSet和TreeSet都是Java中的集合类,但它们有一些不同之处。HashSet是基于哈希表实现的,它不保证元素的顺序,而TreeSet是基于红黑树实现的,它可以保证元素按照升序排列。此外,HashSet的插入、删除和查找操作都是O(1)的,而TreeSet的这些操作则是O(log n)的。因此,如果你需要快速的插入、删除和查找操作,并且不需要保证元素的顺序,那么HashSet是更好的选择;如果你需要保证元素的顺序,那么你应该选择TreeSet。
相关问题
/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; } } }请帮我修改
在 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);
}
}
}
```
基于java.util.HashSet类,创建一组Point3d类型的对象(将该类的源码转至当前项目)。 执行 1000 次迭代,将整数坐标在 0 到 4 之间的随机点添加到创建的集合中。 使用 size() 方法显示集合的维度,使用 contains 方法检查结果集合中是否包含坐标为 (0,0,0) 的点。 注意力! 为了使该方法正常工作,需要按照 Java 约定在 Point3d 类中实现 equals() 和 hashCode() 方法。 根据自己选择的方法实现并演示,使用Iterator接口和相应的方法来解决问题。 将生成的集合及其元素的数量打印到控制台。 使用 java.util.TreeSet 类,创建一组初始集合对象(为此您需要 TreeSet(Collection c))构造函数 注意力! 要使用 TreeSet 集合,有必要在 Point3d 类中实现 Comparable 接口,按距原点的距离(欧几里得或“曼哈顿”,您选择)设置顺序。 将生成的集合及其元素的数量打印到控制台。 编写一个将点集合 (Set<Point3d>) 作为参数的方法。 该方法必须从集合中删除三个坐标都匹配的所有点。
好的,根据您的要求,我来给您提供一份相应的 Java 代码。请注意,为了让代码能够正常运行,您需要在 Point3d 类中实现 equals() 和 hashCode() 方法。同时,由于我无法获取您的完整代码,以下代码仅供参考,您可能需要对其进行适当修改才能让其在您的项目中运行。
```java
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
// 创建 HashSet 集合对象
Set<Point3d> hashSet = new HashSet<>();
// 循环添加随机点
Random random = new Random();
for (int i = 0; i < 1000; i++) {
int x = random.nextInt(5);
int y = random.nextInt(5);
int z = random.nextInt(5);
Point3d point = new Point3d(x, y, z);
hashSet.add(point);
}
// 显示集合的维度和是否包含指定点
System.out.println("Hash Set Dimension: " + hashSet.size());
Point3d targetPoint = new Point3d(0, 0, 0);
System.out.println("Hash Set Contains Target Point: " + hashSet.contains(targetPoint));
// 使用 Iterator 遍历集合
Iterator<Point3d> iterator = hashSet.iterator();
System.out.println("Hash Set Elements:");
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// 创建 TreeSet 集合对象
Set<Point3d> treeSet = new TreeSet<>(hashSet);
// 显示集合的维度
System.out.println("Tree Set Dimension: " + treeSet.size());
// 使用 Iterator 遍历集合
iterator = treeSet.iterator();
System.out.println("Tree Set Elements:");
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// 删除三个坐标都匹配的所有点
removePoints(treeSet);
}
public static void removePoints(Set<Point3d> set) {
Set<Point3d> toRemove = new HashSet<>();
for (Point3d point : set) {
if (point.x == point.y && point.y == point.z) {
toRemove.add(point);
}
}
set.removeAll(toRemove);
}
}
class Point3d implements Comparable<Point3d> {
int x;
int y;
int z;
public Point3d(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Point3d) {
Point3d other = (Point3d) obj;
return this.x == other.x && this.y == other.y && this.z == other.z;
}
return false;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + x;
result = 31 * result + y;
result = 31 * result + z;
return result;
}
@Override
public int compareTo(Point3d other) {
double distance1 = Math.sqrt(x * x + y * y + z * z);
double distance2 = Math.sqrt(other.x * other.x + other.y * other.y + other.z * other.z);
return Double.compare(distance1, distance2);
}
@Override
public String toString() {
return "(" + x + ", " + y + ", " + z + ")";
}
}
```
在以上代码中,我们首先创建了一个 HashSet 集合对象,并循环添加了 1000 个随机点。然后,我们使用 size() 方法和 contains() 方法分别显示了集合的维度和是否包含指定点。接着,我们使用 Iterator 遍历了集合,并将其中的元素输出到控制台。
接下来,我们使用 TreeSet 构造函数创建了一个 TreeSet 集合对象,并将 HashSet 集合对象作为参数传递给它。这样做可以将 HashSet 中的元素自动按照指定的比较规则排序,并将排序后的元素添加到 TreeSet 中。我们同样使用 size() 方法和 Iterator 遍历了 TreeSet 集合,并将其中的元素输出到控制台。
最后,我们编写了一个名为 removePoints() 的方法,该方法接受一个点集合作为参数,并删除其中三个坐标都匹配的所有点。我们使用 HashSet 集合来存储需要删除的点,然后使用 removeAll() 方法从原集合中删除这些点。