用HashSet的add方法谈hashcode和equals方法重写
本文主要通过用HashSet的add方法讲一下hashCode和equals方法重写。错误的地方望指正。
1.了解HashSet的add方法
了解一个方法的好办法是看源码,所以先看源码
private transient HashMap<E,Object> map;
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
由上面可以知道HashSet里面是用的HashMap处理,add方法其实是用了map的put方法
transient Entry<K,V>[] table;
transient int modCount;
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key);
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
modCount++;
addEntry(hash, key, value, i);
return null;
}
table即前面已经存在的数据,这里会将已存在的内容的key与当前key做比较,e.hash == hash && ((k = e.key) == key || key.equals(k)),其中hash是根据hashcode计算,一般情况下,hashCode一致,hash也是一样的。如果比较的是true的话,将已存在的value改成新的。对于hashset其实就原值没变只是在内部的hashmap的value重新放了new Object().
2.例子
到这里我想应该都知道,如何来重写了。这里简单做一个例子。
public class UnlockGood {
public UnlockGood(){}
public UnlockGood(String skuNo, int wmsId,int count) {
super();
this.skuNo = skuNo;
this.count = count;
this.wmsId = wmsId;
}
//商品编码
private String skuNo;
//数量
private int count;
//仓库ID
private int wmsId;
public String getSkuNo() {
return skuNo;
}
public void setSkuNo(String skuNo) {
this.skuNo = skuNo;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getWmsId() {
return wmsId;
}
public void setWmsId(int wmsId) {
this.wmsId = wmsId;
}
@Override
public boolean equals(Object obj) {
UnlockGood good = (UnlockGood)obj;
if(this==obj){
return true;
}else if(this.getSkuNo().equals(good.getSkuNo())&&this.getWmsId()==good.getWmsId()){
good.setCount(good.getCount()+this.getCount());
return true;
}else{
return false;
}
}
@Override
public int hashCode() {
//随便写一个hashCode
return 1;
}
}
上面是重写的hashCode和equals方法,下面是main方法
Set<UnlockGood> unlockgoods = new HashSet<UnlockGood>();
UnlockGood good = new UnlockGood("a",1,2);
unlockgoods.add(good);
UnlockGood good1 = new UnlockGood("a",1,12);
unlockgoods.add(good1);
UnlockGood good2 = new UnlockGood("b",1,2);
unlockgoods.add(good2);
这里利用的是如果hash一致的时候,会调用equals方法,当然如果是同一个key就不会调用equals方法的。利用这个特性对原始值进行修改,达到自己想要的元素加入规则。
用HashSet的add方法谈hashcode和equals方法重写的更多相关文章
- Object 方法的 hashCode,equals方法源码
文章目录 hashCode方法注释 equals 方法注释 equals 方法 hashCode方法注释 Object 的 hashCode 方法,是本地方法: Returns a hash code ...
- 使用hashCode()和equals()方法 - Java
在这篇文章中,我将指出我对hashCode()和equals()方法的理解.我将讨论它们的默认实现以及如何正确地覆盖它们.我还将使用Apache Commons包中的实用工具类来实现这些方法. has ...
- 关于HashCode和equals方法在HashSet中的使用
Object类是类层次结构的根类,故所有的类都是先该类的方法,其中HashCode()和equals()方法也是该类的方法. 1.HashCode()方法 Object类中HashCode()方法实现 ...
- Java 中正确使用 hashCode 和 equals 方法
在这篇文章中,我将告诉大家我对hashCode和equals方法的理解.我将讨论他们的默认实现,以及如何正确的重写他们.我也将使用Apache Commons提供的工具包做一个实现. 目录: hash ...
- 【转】 如何重写hashCode()和equals()方法
转自:http://blog.csdn.net/neosmith/article/details/17068365 hashCode()和equals()方法可以说是Java完全面向对象的一大特色.它 ...
- hashcode和equals方法的区别和联系
说到 hashcode就要和Java中的集合,HashSet,HashMap 关系最为密切. 首先附录两张Java的集合结构图: 图二:(上图的简化版) 从Set集合的特点说起 & Set是如 ...
- (转)Java 中正确使用 hashCode 和 equals 方法
背景:最近在编写持久化对象时候遇到重写equals和hashCode方法的情况,对这两个方法的重写做一个总结. 链接:https://www.oschina.net/question/82993_75 ...
- 如何重写hashCode()和equals()方法
hashCode()和equals()方法可以说是Java完全面向对象的一大特色.它为我们的编程提供便利的同时也带来了很多危险.这篇文章我们就讨论一下如何正解理解和使用这2个方法. 如何重写equal ...
- 重写Java Object对象的hashCode和equals方法实现集合元素按内容判重
Java API提供的集合框架中Set接口下的集合对象默认是不能存储重复对象的,这里的重复判定是按照对象实例句柄的地址来判定的,地址相同则判定为重复,地址不同不管内容如何都判定为不重复,这有时与需求不 ...
随机推荐
- jspace2d——A free 2d multiplayer space shooter
http://code.google.com/p/jspace2d/ —————————————————————————————————————————————————————————————— We ...
- hdu 5655 CA Loves Stick
CA Loves Stick Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- ASP.NET MVC- 解决HTML转码
在MVC里从Controller发送一段带有HTML的文字到View视图时,MVC是会将这段代码进行转码的. 一.使用MvcHtmlString转HTML转码 如果想不让这段文字进行转码,以HTML的 ...
- ASP.NET基础之HttpModule 、HttpContext、 HttpHandler
http://www.cnblogs.com/wujy/p/3261141.html http://www.cnblogs.com/wujy/p/3264475.html http://www.cnb ...
- HTML to DOM
Although you can now natively parse HTML using DOMParser and XMLHttpRequest, this is a new feature t ...
- $( document ).ready()&$(window).load()
$( document ).ready() https://learn.jquery.com/using-jquery-core/document-ready/ A page can't be man ...
- A debugger is already attached
Today is the last day that all the laptops of winXP OS should be upgrade to WIN7. After updated. whe ...
- 广州项目实施步骤I_练习安装 CentOS x64 6.4
安装Centos x64 6.4 在家里使用 Vmware10.0.1进行模拟安装. 永久KEY注册密钥:5F29M-48312-8ZDF9-A8A5K-2AM0Z 下载地址:http://pan. ...
- C++ 转型
1.const_static的使用场景:接收一个const对象,但是想改变对象内容,使用const_static去除对象的常量性,然后可以修改对象. 2.dynamic_static的使用场景:从子类 ...
- hdu 5592 ZYB's Game 树状数组
ZYB's Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=55 ...