除了String 和 Integer 重写了 equals 方法,还有吗
时间: 2024-03-29 18:39:22 浏览: 22
除了 `String` 和 `Integer`,还有一些其他的 Java 类也重写了 `equals` 方法,例如 `Boolean`,`Character`,`Double`,`Float`,`Long` 等基本数据类型的包装类。此外,还有一些常用的类如 `Date`,`File`,`List`,`Map` 等也重写了 `equals` 方法。值得注意的是,对于自定义的类,如果需要比较对象的内容而不是引用地址,也需要重写 `equals` 方法。
相关问题
类中有属性:carName String,carType String,carprice Double,carcount Integer,carDate Date,封装这个类,重写toString()方法和equals()方法
可以封装一个名为`Car`的类,该类包含属性`carName`、`carType`、`carPrice`、`carCount`和`carDate`。以下是一个示例实现:
```java
import java.util.Date;
public class Car {
private String carName;
private String carType;
private Double carPrice;
private Integer carCount;
private Date carDate;
public Car(String carName, String carType, Double carPrice, Integer carCount, Date carDate) {
this.carName = carName;
this.carType = carType;
this.carPrice = carPrice;
this.carCount = carCount;
this.carDate = carDate;
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public String getCarType() {
return carType;
}
public void setCarType(String carType) {
this.carType = carType;
}
public Double getCarPrice() {
return carPrice;
}
public void setCarPrice(Double carPrice) {
this.carPrice = carPrice;
}
public Integer getCarCount() {
return carCount;
}
public void setCarCount(Integer carCount) {
this.carCount = carCount;
}
public Date getCarDate() {
return carDate;
}
public void setCarDate(Date carDate) {
this.carDate = carDate;
}
@Override
public String toString() {
return "Car{" +
"carName='" + carName + '\'' +
", carType='" + carType + '\'' +
", carPrice=" + carPrice +
", carCount=" + carCount +
", carDate=" + carDate +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Car car = (Car) o;
if (!carName.equals(car.carName)) return false;
if (!carType.equals(car.carType)) return false;
if (!carPrice.equals(car.carPrice)) return false;
if (!carCount.equals(car.carCount)) return false;
return carDate.equals(car.carDate);
}
@Override
public int hashCode() {
int result = carName.hashCode();
result = 31 * result + carType.hashCode();
result = 31 * result + carPrice.hashCode();
result = 31 * result + carCount.hashCode();
result = 31 * result + carDate.hashCode();
return result;
}
}
```
这个`Car`类包含了构造方法用于初始化属性,以及各个属性的getter和setter方法。重写了`toString()`方法,可以将对象转换为字符串表示。重写了`equals()`方法,用于比较两个对象是否相等。
你可以根据需要创建`Car`对象,并使用相应的方法进行属性的访问和操作。例如:
```java
Car car = new Car("BMW", "Sedan", 50000.0, 10, new Date());
System.out.println(car.toString());
```
这将打印出`Car`对象的字符串表示。你还可以使用`equals()`方法来比较两个`Car`对象是否相等。
为什么HashSet自定义对象不会去重?而定义String和Integer类型的就会去重?set集合不是是一个无序不重复的集合吗?
HashSet是一个基于哈希表实现的集合,它通过计算每个对象的哈希码来确定存储位置。当我们向HashSet中添加元素时,它会首先检查该元素的哈希码是否已经存在于集合中,如果存在则会比较元素的值是否相等,如果值相等,则认为是重复元素,不会被添加进集合。
对于String和Integer类型的对象,Java已经为它们重写了hashCode()和equals()方法。这意味着对于相同值的String或Integer对象,它们的哈希码是相等的,并且equals()方法会比较它们的值是否相等。因此,当我们将String或Integer对象添加到HashSet中时,会根据哈希码来确定存储位置,并通过equals()方法判断是否已经存在相同值的对象,如果存在则不会重复添加。
然而,对于自定义的对象,默认情况下,Java使用对象的引用地址来计算哈希码和比较对象的相等性。即使两个对象的字段值相同,但它们的引用地址不同,HashSet会认为它们是不同的对象,并将它们都添加进集合中。为了让HashSet正确去重自定义对象,我们需要在自定义对象中重写hashCode()和equals()方法,使其根据对象的字段值来计算哈希码和比较相等性。这样HashSet才能正确判断两个自定义对象是否相等,并进行去重操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)