Let's say I have this type in my application:
public class A { public int id; public B b; public boolean equals(Object another) { return this.id == ((A)another).id; } public int hashCode() { return 31 * id; //nice prime number } }
and a Set
structure. Now, I have an object of type <A
>A
and want to do the following:
A
is within the set, update its field b
to match my object.So checking if it is in there is easy enough (contains
), and adding to the set is easy too. My question is this: how do I get a handle to update the object within? Interface Set
doesn't have a get
method, and the best I could think of was to remove the object in the set and add mine. another, even worse, alternative is to traverse the set with an iterator to try and locate the object.
I'll gladly take better suggestions... This includes the efficient use of other data structures.
Yuval =8-)
EDIT: Thank you all for answering... Unfortunately I can't 'accept' the best answers here, those that suggest using a Map
, because changing the type of the collection radically for this purpose only would be a little extreme (this collection is already mapped through Hibernate...)
Generally, collections with some kind of internal structure don't watch for changes in their elements, and their structure will be destroyed if you modify the elements (in ways that change the property that the structure is based on). This holds for TreeSet as well.
Set add() method in Java with ExamplesThe add() method of Set in Java is used to add a specific element into a Set collection. The function adds the element only if the specified element is not already present in the set else the function return False if the element is already present in the Set.
The speed and direction of motion can be changed by applying force on an object and thus a force can bring a change in the state of motion of an object.
Since a Set can only contain one instance of an object (as defined by its equals
and hashCode
methods), just remove it and then add it. If there was one already, that other one will be removed from the Set and replaced by the one you want.
I have code that does something similar - I am caching objects so that everywhere a particular object appears in a bunch of different places on the GUI, it's always the same one. In that case, instead of using a Set I'm using a Map, and then I get an update, I retrieve it from the Map and update it in place rather than creating a new instance.
You really want to use a Map<Integer,A>
, not a Set<A>
.
Then map the ID (even though it's also stored in A
!) to the object. So storing new is this:
A a = ...; Map<Integer,A> map = new HashMap<Integer,A>(); map.put( a.id, a );
Your complete update algorithm is:
public static void update( Map<Integer,A> map, A obj ) { A existing = map.get( obj.id ); if ( existing == null ) map.put( obj.id, obj ); else existing.b = obj.b; }
However, it might be even simpler. I'm assuming you have more fields than that in A
that what you gave. If this is not the case, just using a Map<Integer,B>
is in fact what you want, then it collapses to nothing:
Map<Integer,B> map = new HashMap<Integer,B>(); // The insert-or-update is just this: map.put( id, b );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With