为什么在HashMap中使用键检索这些值?(Why are these values retrieved with keys in HashMap? [duplicate])
这个问题在这里已有答案:
- 可变的hashmap键是危险的做法吗? 7个答案
当我在HashMap上运行以下代码时,我正在努力解释为什么我将第二行输出作为“Line2:null”:
import java.util.*; class Dog { public Dog(String n) {name = n;} public String name; public boolean equals(Object o) { if((o instanceof Dog) && (((Dog)o).name == name)) { return true; } else {return false;} } public int hashCode() {return name.length();} } public class HelloWorld{ public static void main(String []args){ Map<Object, Object> m = new HashMap<Object, Object>(); Dog d1 = new Dog("clover"); m.put(d1, "Dog key"); System.out.println("Line1: " + m.get(d1)); d1.name = "magnolia"; System.out.println("Line2: " + m.get(d1)); d1.name = "clover"; System.out.println("Line3: " + m.get(new Dog("clover"))); d1.name = "arthur"; System.out.println("Line4: " + m.get(new Dog("clover"))); } }
显示的输出是:
Line1:狗钥匙
第2行:null
Line3:狗钥匙
第4行:null
是的,我确实意识到,由于我计算哈希码的方式,修改实例变量名反过来会影响Dog实例的哈希码。 但是,我使用相同的实例作为密钥! 那么,为什么get()方法不能找到相应的值呢? 看起来,一旦将一对推入HashMap,关键就是永久硬编码! 这是它应该如何工作,这意味着,一旦在HashMap中放置该对之前确定了一个值的哈希码,就不能再次修改哈希码?
This question already has an answer here:
I am struggling with the reason why I am getting the second line of outputs as "Line2: null" when running the following piece of code on HashMap:
import java.util.*; class Dog { public Dog(String n) {name = n;} public String name; public boolean equals(Object o) { if((o instanceof Dog) && (((Dog)o).name == name)) { return true; } else {return false;} } public int hashCode() {return name.length();} } public class HelloWorld{ public static void main(String []args){ Map<Object, Object> m = new HashMap<Object, Object>(); Dog d1 = new Dog("clover"); m.put(d1, "Dog key"); System.out.println("Line1: " + m.get(d1)); d1.name = "magnolia"; System.out.println("Line2: " + m.get(d1)); d1.name = "clover"; System.out.println("Line3: " + m.get(new Dog("clover"))); d1.name = "arthur"; System.out.println("Line4: " + m.get(new Dog("clover"))); } }
The outputs displayed are:
Line1: Dog key
Line2: null
Line3: Dog key
Line4: null
Yes, I do realize that modifying the instance variable name, in turn, affects the hashcode of the instance of Dog because of the way I calculate the hashcode. But, I am using the same instance as the key! So, why cannot the get() method find the corresponding value? It seems like once a pair is pushed into a HashMap, the key is hardcoded with the value forever! Is this how it is supposed to work, meaning that, once a hashcode has been determined for a value before placing the pair in HashMap, the hashcode can never be modified again?
原文:https://stackoverflow.com/questions/30955953
最满意答案
您可以将AutoPersistenceModelGenerator中的GetSetup()方法更改为如下所示:
private Action<AutoMappingExpressions> GetSetup() { return c => { c.IsComponentType = type => type.BaseType == typeof (ValueObject); }; }
我会尽力让我看到的这篇博客帖子涵盖了这个信誉。
You could change the GetSetup() method in AutoPersistenceModelGenerator to something like:
private Action<AutoMappingExpressions> GetSetup() { return c => { c.IsComponentType = type => type.BaseType == typeof (ValueObject); }; }
I'll try to get the blogpost I saw covering this posted for credit.
相关问答
更多-
流畅的Nhibernate映射 - 值对象内的一对多?(Fluent Nhibernate Mapping - one-to-many inside a Value Object?)[2023-05-23]
好。 我终于解决了。 现在,我的WorkEvaluation对象可以使用包含权重和命中间隔列表的QuestionScoreTuning对象(值对象)来应用。 事实证明这很好,如果有人想要更多关于在价值对象中设置集合并将它们映射到流利NH的信息,请在这里提出评论。 我可以提供代码示例...... Well. I Finally solved it. Now my WorkEvaluation object can be Applied with a QuestionScoreTuning object (a ... -
您可以将AutoPersistenceModelGenerator中的GetSetup()方法更改为如下所示: private Action
GetSetup() { return c => { c.IsComponentType = type => type.BaseType == typeof (ValueObject); ... -
你如何用Fluent NHibernate自动化List
或float []?(How do you automap List [2022-02-11]or float[] with Fluent NHibernate?) 自从我发布了我的问题以来,Fluent NHibernate团队已经解决了这个问题。 您现在可以自动化C#值类型的IList(字符串,整数,浮点数等)。 只需确保您有最新版本的FNH。 编辑 我最近从FNH 1.0升级到FNH 1.3。 这个版本也会自动化数组 - float [],int []等。 似乎将它们映射为BLOB。 我认为这比IList更有效,但没有做任何分析来确认。 Since I posted my question, the Fluent NHibernate team have fixe ... -
与property-ref有关,请参阅NHibernate通过session.Load()创建代理,但不能通过Linq或Criteria API创建代理 而不是你问过,但也考虑到代理多对一不允许你进行类型检查,如果你是用户的子类,请参阅http://ayende.com/Blog/archive/2010/01/28/ NHibernate的新特征-无代理associations.aspx Has to do with property-ref see NHibernate creates proxy vi ...
-
我弄清楚了! 有两个问题。 我的控制器中的PUT动词包括声明中的“int id”。 这使得默认路由混乱,因为在帖子期间URL没有包含ID。 将PUT声明更改为以下问题可解决此问题: public IHttpActionResult PutUser(UserProfile userProfile) 由于输入错误,我的控制器中的DELETE动词没有从邮递员或angularjs获得正确的输入。 自我注意:获得更多睡眠并观看这些URL。 这就是我的工作控制器的外观: using System; using ...
-
List错误是由于List的语义而发生的。 这很麻烦,因为在默认情况下bag也会映射到IList ,但这是由于在.NET框架中缺少具有“bag”语义的集合类型。 在概念上,“袋子”中没有订单,因为物品只是被扔进包里并以任何顺序被移走,并且很容易在包内移动。 然而,一个列表是固有的排序和索引(把它看作是一个带有附加语义的数组,因此每个元素都有唯一的索引值)。 你得到的错误信息表明NHibernate需要一个存储每个元素索引值的列(所以它必须是唯一的,因为它是一个IList ,蚂蚁整数值)。 由于您似乎没有索引 ...
-
要使用NHibernate对CultureInfo的映射支持(请参阅https://github.com/nhibernate/nhibernate-core/blob/c85d038dce8ba87bd3f4de2458b4ef6e2497f7f8/src/NHibernate/Type/CultureInfoType.cs ),您需要告诉Fluent NHibernate您想通过以下约定使用它: using System.Globalization; using FluentNHibernate.Co ...
-
sessionfactory尝试根据此指令Add(AutoMap.AssemblyOf
(cfg))包含您的Agency类的程序集中的所有类: Add(AutoMap.AssemblyOf (cfg)) 。 由于您在程序ClassMap<>有一个AgencyMap而ClassMap<>没有Id属性,因此FNH会抛出错误。 如果要使用ClassMap<>配置,而不是(或除此之外)声明自动化配置,请声明一个流畅的映射: m.FluentMappings.AddFromAssembly ... -
在Fluent NHibernate Automap Override中定义唯一列(Defining unique column in Fluent NHibernate Automap Override)[2023-10-06]
简短的回答,你想要的是你必须在代码中处理的东西,因为有很多可能性。 每次创建新的CodeType ,都必须检查db(如果已经存在) SecurableEntityRepositoryctr = new SecurableEntityRepository (); CodeType ct = ctr.GetByType("existingType"); if (ct == null) { ct = new CodeType { type = "existingTy ... -
在MySQL中,您可以创建一个列并将其属性提及为AUTO_INCREMENT和DEFAULT VALUE 1(或任何您想要的),为什么不使用它? - 干杯 In MySQL you can create a column and mention its properties as AUTO_INCREMENT and DEFAULT VALUE 1 (or whatever you wish), why don't you use it? --Cheers