容器类自定义类型进行比较的时候务必重写equals方法务必要重写hashCode方法
package com.study;
import java.util.*;
public class OverrideEquaslCode {
public static void main(String []args) {
Collection c2= new HashSet();
c2.add("Colleciton Hello1");
c2.add("Colleciton Hello2");
c2.add("Colleciton Hello3");
c2.add(new ComputerClass("Computer1","100,200"));
c2.add(new ComputerClass("Computer2","200,300"));
System.out.println(c2);
c2.remove("Colleciton Hello3");
System.out.println(c2); // 可以删除,基础类型继承了Object基类的equals()方法
//下面的输出为false,因为没有自定义类型需要重写equals()方法和hashCode()方法且hashCode()比较键值对.
System.out.println(c2.remove(new ComputerClass("Computer1","100,200"))); //重写equals方法务必要重写hashCode方法
System.out.println(c2);
}
}
class ComputerClass {
private String brand_name;
private String properties;
public ComputerClass(String brand_name,String properties) {
this.brand_name=brand_name;
this.properties=properties;
}
public boolean equals(Object obj) {
ComputerClass cClass = (ComputerClass)obj;//强制类型转换
if(obj instanceof ComputerClass) { //instaceof 比较左边对象是否是它右边的类的实例
return brand_name.equals(cClass.brand_name) && properties.equals(cClass.properties);
}
else {
return super.equals(obj); //否则交给父类进行比较
}
}
public int hashCode() {
return brand_name.hashCode();
}
public String getBrand_name() {
return brand_name;
}
public void setBrand_name(String brand_name) {
this.brand_name = brand_name;
}
public String getProperties() {
return properties;
}
public void setProperties(String properties) {
this.properties = properties;
}
}
分享到:
相关推荐
重写equals和hashcode方法,学习和进步
Java重写equals同时需要重写hashCode的代码说明,以及如何重写hashCode方法,此代码演示按照effective java书籍说明的重写思路。代码中演示了使用集合存储对象,并且对象作为key,需重写equals和hashCode.
关于重写equals,hashcode以及compareTo方法!
本文还介绍了定义对象的相等性、实施equals()和hashCode()的需求、编写自己的equals()和hashCode()方法。通过统一定义equals()和hashCode(),可以提升类作为基于散列的集合中的关键字的使用性。
NULL 博文链接:https://zpointer.iteye.com/blog/1058337
文章目录1、hashCode与equals两者之间的关系2、== 和equals的区别`3、为什么要重写equals()方法?4、重写equals()方法5、为什么要重写hashCode()方法?6、什么时候需要重写hashCode()方法?7、重写hashCode()方法: ...
主要介绍了Java重写equals及hashcode方法流程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了重写hashCode()和equals()方法详细介绍,涉及重写equals()方法,重写hashCode()方法,重写equals()而不重写hashCode()的风险等相关内容的介绍,具有一定借鉴价值,需要的朋友可以参考下
NULL 博文链接:https://alpha2009.iteye.com/blog/570322
面试官瞬间就饱了,重写equals函数,需要重写hashCode函数吗? 面试官问我,为什么重写equals函数,必须重写hashCode函数,我当时就懵住了。 然后扯天扯地,然后面试官瞬间就饱了,痛定思痛,写下这篇博客 首先看...
这里是一个文档,里边讲解了hashCode与equals方法使用,大家要是不明白,可以去看看
Java容器集合(equals 和 hashCode+基础数据结构+ArrayList+Vector和LinkedList)
解析Java对象的equals()和hashCode()的使用
原因是因为,在Java自带的容器HashMap和HashSet中, 都需同时要用到对象的hashCode()和equals()方法来进行判断,然后再插入删除元素,这点我们一会再谈。 那么我们还是单独来看hashCode(),为什么HashMap需要用到...
本文讲述了什么时候重写equals方法和如何重写equals方法。
为什么重写equals方法,还必须要重写hashcode方法
equals()和hashcode()这两个方法都是从object类中继承过来的。当String 、Math、还有Integer、Double。。。。等这些封装类在使用equals()方法时,已经覆盖了object类的equals()方法.
要注意的是List,Set,Queue继承了Collection接口,...这里想用一个简单的例子展示一下他们的使用,内容包括:List、Map、Set、Queue,Collections、Comparable与Comparator,排序、搜索,内部类,泛型、重写equals、hashCode
个人学习终结成果:为什么要重新equals和hashCode方法?如何重写?站好马步需从j2se基础开始
首先我们先来看下String类的源码:可以发现String是重写了Object类的equals方法的,并且也重写了hashcode方法