Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating an object within a Set

Tags:

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<A> structure. Now, I have an object of type A and want to do the following:

  • If my A is within the set, update its field b to match my object.
  • Else, add it to the set.

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

like image 607
Yuval Avatar asked Oct 06 '08 16:10

Yuval


People also ask

Can you modify the object set in Java?

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.

Can we add object in set Java?

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.

How do we change the state of an object?

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.


2 Answers

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.

like image 97
Paul Tomblin Avatar answered Dec 29 '22 00:12

Paul Tomblin


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 ); 
like image 40
Jason Cohen Avatar answered Dec 28 '22 22:12

Jason Cohen