¿Qué debo usar para rastrear detalles de objetos mutables en java?

I want to store credit history information about people objects in a simulation.

Yo quería usar 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).

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

Is there a better structure for this?

preguntado el 10 de marzo de 12 a las 00:03

The docs sometimes say weird things...It's not really a probably unless you plan on modifying the keys. The values can be modified all you want. -

just send the credit information to me I'll keep track of it. -

2 Respuestas

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. 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.

respondido 10 mar '12, 01:03

I do have to modify Person objects. Should I just override the hashCode() function from Object() to return the person's String uniqueID? Or, alternately, I could use String uniqueID as the key to the map - bernie2436

Generally, using complex mutable objects as keys is not a good idea. Use something immutable that is a unique identifier for the VALORAMOS. In your case, if the uniqueID of a person cannot change then it's a good candidate. - Jim Garrison

Unless you plan on modifying the Person objects often, you should be okay.

The API says to be wary of using mutable objects as keys. 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.

respondido 10 mar '12, 01:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.