简体   繁体   English

我应该使用什么来跟踪Java中可变对象的详细信息

[英]what should I use to track details of mutable objects in java

I want to store credit history information about people objects in a simulation. 我想在模拟中存储有关人员对象的信用历史记录信息。

I wanted to use HashMap<Person, CreditHistory> , but the java API says I should be wary about using HashMaps to track mutable objects (like credit history, in my program). 我想使用HashMap<Person, CreditHistory> ,但是Java API说我应该警惕使用HashMaps跟踪可变对象(例如程序中的信用记录)。

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/HashMap.html http://docs.oracle.com/javase/1.5.0/docs/api/java/util/HashMap.html

Is there a better structure for this? 有没有更好的结构呢?

The caution applies only to keys -- If you change anything about the key that affects hashCode() or equals() behavior, you break the hash mapping and won't be able to find the key again. 该警告仅适用于键-如果您更改了任何会影响hashCode()equals()行为的键,则将破坏哈希映射,并且将无法再次找到该键。 For the values, you're free to do anything you want. 对于这些值,您可以随意做任何事情。 It'll be up to you if modifying a value changes the semantics of your map. 如果修改值会更改地图的语义,则取决于您。

Unless you plan on modifying the Person objects often, you should be okay. 除非您计划经常修改Person对象,否则您应该没事。

The API says to be wary of using mutable objects as keys. API说要警惕使用可变对象作为键。 This is because strict logic might fail when the key change. 这是因为更改密钥时严格的逻辑可能会失败。 If you program it right, you'll be fine. 如果编程正确,那就没问题了。

If you're just changing the CreditHistory objects, this is the best structure and (in my opinion) the easiest to use. 如果您只是在更改CreditHistory对象,那么这是最好的结构,并且(我认为)最容易使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM