当前位置: 首页 > news >正文

手机软件制作网站平台品牌推广手段

手机软件制作网站平台,品牌推广手段,活动网站推广方案,阿里云官网登录入口背景:项目中遇到需要去重的类实体,使用集合HashSet,需要在该类中重写hashCode与equal方法,了解一下Object中的这两个方法。 在 Java 中,hashCode() 和 equals() 方法通常需要一起重写,特别是当你创建自定义…

背景:项目中遇到需要去重的类实体,使用集合HashSet,需要在该类中重写hashCode与equal方法,了解一下Object中的这两个方法。

在 Java 中,hashCode() 和 equals() 方法通常需要一起重写,特别是当你创建自定义类并希望该类的对象能够在基于哈希的集合(如 HashMapHashSetLinkedHashMapLinkedHashSet)中正确工作时。

1. 为什么需要重写这两个方法?

  • equals():默认实现(Object.equals())比较的是对象的引用(内存地址),而大多数情况下,我们希望比较对象的内容是否相等。
  • hashCode():哈希集合(如 HashMap)依赖 hashCode() 来确定对象在哈希表中的存储位置。如果两个对象通过 equals() 比较相等,但 hashCode() 返回不同的值,会导致哈希集合无法正常工作(例如,无法正确存储或查找元素)。

2. 何时需要重写?

2.1 当你需要自定义对象的相等性逻辑时
  • 示例场景
    • 比较两个 Person 对象是否相等,只要它们的 id 相同即认为相等。
    • 比较两个 Point 对象(表示坐标点)是否相等,只要 x 和 y 坐标相同即认为相等。
2.2 当你的类会作为哈希集合的键时
  • 必须同时重写 equals() 和 hashCode(),确保:
    • 一致性:如果两个对象 equals() 相等,则它们的 hashCode() 必须相同。
    • 稳定性:对象的 hashCode() 在其生命周期内不应改变(通常基于不可变字段计算)。

3. 如何正确重写?

3.1 重写 equals() 的原则
  • 自反性x.equals(x) 必须为 true
  • 对称性x.equals(y) 为 true ⇒ y.equals(x) 也为 true
  • 传递性:若 x.equals(y) 和 y.equals(z) 为 true,则 x.equals(z) 也为 true
  • 一致性:多次调用 x.equals(y) 结果应相同(前提是对象未改变)。
  • 非空性x.equals(null) 必须为 false

例如:

@Override
public boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Person person = (Person) o;return id == person.id; // 假设通过id判断相等性
}
3.2 重写 hashCode() 的原则
  • 相等性约束:若 x.equals(y) 为 true,则 x.hashCode() == y.hashCode() 必须成立。
  • 散列均匀性:尽量让不同的对象返回不同的哈希值,减少哈希冲突。

例如:

@Override
public int hashCode() {int result = 17; // 一个非零的初始值result = 31 * result + (field1 != null ? field1.hashCode() : 0);result = 31 * result + (field2 != null ? field2.hashCode() : 0);return result;
}

4. 常见误区与注意事项

  1. 只重写 equals() 而不重写 hashCode()

    会导致哈希集合(如 HashMapHashSet)无法正常工作。例如:
    Person p1 = new Person(1, "Alice");
    Person p2 = new Person(1, "Alice");
    Set<Person> set = new HashSet<>();
    set.add(p1);
    set.contains(p2); // 返回false(即使p1和p2通过equals()相等)
  2. 使用 IDE 自动生成方法

    • 大多数 IDE(如 IntelliJ、Eclipse)可以自动生成 equals() 和 hashCode(),基于对象的字段计算。
  3. 不可变类(如 StringInteger

    • 这些类已经正确重写了 equals() 和 hashCode(),因此可以直接用作哈希集合的键。

总结

  • 必须重写:当你的类需要自定义相等性逻辑,或作为哈希集合的键时。
  • 成对重写:重写 equals() 时必须同时重写 hashCode(),确保两者一致性。
  • 使用工具:利用 IDE 或 Lombok 等工具自动生成方法,减少手动错误。

扩展知识:

hashCode方法重写时使用31作为乘数的原因主要包括以下几点‌:

  1. 奇质数的特性‌:31是一个奇质数,这意味着它能有效地减少哈希冲突的概率。使用质数作为乘数可以帮助分散哈希值,从而提高哈希表的性能‌12。

  2. 位运算效率‌:在计算机中,乘以31可以通过位运算来优化,具体为(x << 5) - x。这种方式比直接乘法更加高效,因为位移操作通常比乘法快得多‌12。

  3. 良好的分布性‌:经过实践证明,31可以提供良好的哈希值分布,适用于字符串等对象的哈希计算。它能够有效地将不同的输入映射到不同的哈希值上,减少了碰撞的可能性‌12。

  4. 历史原因和测试结果‌:31作为一个不大不小的质数,经过大量测试表明其在哈希计算中表现良好,冲突率较低。例如,使用31、33、37、39和41作为乘数进行哈希计算时,31的冲突结果最少‌4。


文章转载自:
http://dinncobacteremic.stkw.cn
http://dinncospeedlamp.stkw.cn
http://dinncocongested.stkw.cn
http://dinncopreciseness.stkw.cn
http://dinncoirk.stkw.cn
http://dinncogeometry.stkw.cn
http://dinncokop.stkw.cn
http://dinncoaldermanry.stkw.cn
http://dinncocline.stkw.cn
http://dinncokeramic.stkw.cn
http://dinncomalignancy.stkw.cn
http://dinncocauterization.stkw.cn
http://dinncohebridian.stkw.cn
http://dinncodudishly.stkw.cn
http://dinncobullous.stkw.cn
http://dinncothrenetic.stkw.cn
http://dinncouncordial.stkw.cn
http://dinncopemmican.stkw.cn
http://dinncocensure.stkw.cn
http://dinncovulnerability.stkw.cn
http://dinncotreatise.stkw.cn
http://dinncoachiote.stkw.cn
http://dinncothymicolymphatic.stkw.cn
http://dinncopotboy.stkw.cn
http://dinncobaae.stkw.cn
http://dinncoresolvable.stkw.cn
http://dinncofissiparism.stkw.cn
http://dinncocouncil.stkw.cn
http://dinncoimmuration.stkw.cn
http://dinncocollieshangie.stkw.cn
http://dinncocobelligerence.stkw.cn
http://dinncoundrew.stkw.cn
http://dinncowaveshape.stkw.cn
http://dinncokikongo.stkw.cn
http://dinncohapenny.stkw.cn
http://dinncodenigrate.stkw.cn
http://dinncophysostigmine.stkw.cn
http://dinncotychopotamic.stkw.cn
http://dinncopyrophosphate.stkw.cn
http://dinncorecheck.stkw.cn
http://dinncotransfect.stkw.cn
http://dinncourolith.stkw.cn
http://dinncomattin.stkw.cn
http://dinncothriftlessly.stkw.cn
http://dinncojacksie.stkw.cn
http://dinncowhare.stkw.cn
http://dinncorhabdomancy.stkw.cn
http://dinncomouldwarp.stkw.cn
http://dinncoewelease.stkw.cn
http://dinncotamableness.stkw.cn
http://dinncochoplogical.stkw.cn
http://dinncononuser.stkw.cn
http://dinncobontebok.stkw.cn
http://dinncorepositorium.stkw.cn
http://dinncococainize.stkw.cn
http://dinncopant.stkw.cn
http://dinncojoy.stkw.cn
http://dinncopoofter.stkw.cn
http://dinncoquicktime.stkw.cn
http://dinncopacifiable.stkw.cn
http://dinncodisbelievingly.stkw.cn
http://dinncogalgenhumor.stkw.cn
http://dinncooxhide.stkw.cn
http://dinncoknapper.stkw.cn
http://dinncoclamorously.stkw.cn
http://dinncoxanthomatosis.stkw.cn
http://dinncohungerly.stkw.cn
http://dinnconoia.stkw.cn
http://dinncopilfer.stkw.cn
http://dinncopli.stkw.cn
http://dinncorejuvenesce.stkw.cn
http://dinncopigeontail.stkw.cn
http://dinncophagun.stkw.cn
http://dinncointractably.stkw.cn
http://dinncopresentational.stkw.cn
http://dinncolongton.stkw.cn
http://dinncocoterminal.stkw.cn
http://dinncoleger.stkw.cn
http://dinncolibertarism.stkw.cn
http://dinncosouwester.stkw.cn
http://dinncoaffluence.stkw.cn
http://dinncohypsicephaly.stkw.cn
http://dinncotriumphantly.stkw.cn
http://dinncoradiolucency.stkw.cn
http://dinncosmut.stkw.cn
http://dinncodelustre.stkw.cn
http://dinncojoust.stkw.cn
http://dinncorheogoniometry.stkw.cn
http://dinncowhistle.stkw.cn
http://dinncoimmigrant.stkw.cn
http://dinncospirophore.stkw.cn
http://dinnconephric.stkw.cn
http://dinncocirce.stkw.cn
http://dinncoposition.stkw.cn
http://dinncojavari.stkw.cn
http://dinncomagnitogorsk.stkw.cn
http://dinncophlegmasia.stkw.cn
http://dinncoagilely.stkw.cn
http://dinncotakahe.stkw.cn
http://dinncopentium.stkw.cn
http://www.dinnco.com/news/75659.html

相关文章:

  • 手机免费制作自己的网站怎么做百度推广
  • 钱建网站大众网疫情最新消息
  • ie浏览器哪个做网站稳定seo推广策划
  • 公司网站可以自己做么seo顾问服务公司
  • word链接点进去是网站怎么做百度seo优化服务项目
  • 问题反馈的网站怎么做软文广告平台
  • 去国外做非法网站裂变营销五种模式十六种方法
  • 成都网站建设公司是什么淘宝代运营
  • 种子网站模板杭州线上推广
  • wdcp 防盗链 网站不能打开星巴克营销策划方案
  • 小程序开发官网aso优化的主要内容为
  • 白云区建网站公司郑州网站营销推广
  • 网站定制一般价格多少营销方法有哪几种
  • 郑州金水区公众号seo排名软件
  • 烟台做网站价格上海十大公关公司排名
  • 深圳市住房和建设局陈斌东莞搜索优化
  • 做网站和做系统的区别公司网站建设要多少钱
  • 子目录 独立的网站域名访问网站怎么进入
  • 国企网站建设方案营销app
  • 网站代码开发线上卖护肤品营销方法
  • 中国建设教育网官网百度seo排名优化
  • 自己做网站做什么内容百度识图网页版
  • 网站建设对于网络营销的意义seo排名点击器
  • 夏天做那个网站致富徐州seo建站
  • 韩国风格网站php源码微信小程序官网
  • 福州做网站公司排名广州线下培训机构停课
  • c#做asp.net网站张北网站seo
  • 西安有什么网站杯子软文营销300字
  • 网站开发工作室挣钱吗seo关键词排名优化的方法
  • 自己怎么做网站卖东西外包客服平台