热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在Java中返回Hashset中的对象-ReturningaObjectinaHashsetinJava

HiIhaveaHashSetlikethefollowinginaclasscalledMemory:嗨,我在一个名为Memory的类中有如下的HashSet:Set&

Hi I have a HashSet like the following in a class called Memory:

嗨,我在一个名为Memory的类中有如下的HashSet:

Set ideas = new HashSet();

The generic type "Idea" is another class I wrote that has hashcode() and equals() overriden. I want to be able to get(and not remove) an Idea object in the HashSet ideas, then change it by adding something to it possibly changing it's hashcode() return value. I heard that this is would not work but no one explained why. I was wondering if someone could tell me how I can do this most efficiently.

泛型类型“Idea”是我编写的另一个类,它具有hashcode()和equals()覆盖。我希望能够在HashSet中获取(而不是删除)一个Idea对象,然后通过添加一些东西来改变它,可能会改变它的hashcode()返回值。我听说这不行,但没有人解释原因。我想知道是否有人能告诉我如何才能最有效地做到这一点。

3 个解决方案

#1


1  

If you want to perform a lookup, you should be using a Map. If you want to change the key (or an element of a Set) you have to remove it first and add it again. For this reason your key should only have immutable fields.

如果要执行查找,则应使用Map。如果要更改键(或Set的元素),则必须先将其删除并再次添加。因此,您的密钥应该只有不可变字段。

#2


2  

HashSet internally uses HasMap with value same as the key.For putting an object to Hashset, jvm fill first calculate the hashcode of the object and based on that hashcode, corresponding bucket is selected and object is put.So if you are changing the hashcode after putting the object into hashset,you wont be able to get its location correctly.So if you really want to remove the element, better remove the object from hashset change its value and then put it back again

HashSet在内部使用HasMap,其值与key相同。为了将对象放入Hashset,jvm fill首先计算对象的hashcode,并根据该hashcode,选择相应的bucket并放置对象。如果你要更改hashcode之后将对象放入hashset,你将无法正确获取其位置。如果你真的想要删除元素,最好从hashset中删除对象更改其值,然后再将其重新放回

#3


1  

It wont work because the hashcode is the key to find the object, if you modify the object in such a way you change it's hashcode you won't be able to find it again.

它不会工作,因为哈希码是查找对象的关键,如果以这种方式修改对象,则更改它的哈希码,您将无法再次找到它。

It is like the entry in a dictionary, if you change it you won't find it again. Does it makes sense?

它就像字典中的条目,如果你改变它,你将不会再找到它。这有道理吗?


推荐阅读
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 深入理解 SQL 视图、存储过程与事务
    本文详细介绍了SQL中的视图、存储过程和事务的概念及应用。视图为用户提供了一种灵活的数据查询方式,存储过程则封装了复杂的SQL逻辑,而事务确保了数据库操作的完整性和一致性。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 本文详细介绍了 Java 中 org.apache.xmlbeans.SchemaType 类的 getBaseEnumType() 方法,提供了多个代码示例,并解释了其在不同场景下的使用方法。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
author-avatar
GRIROR格雷尔
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有