Re: Get reference to object in Set



On Mar 5, 5:24 pm, Knute Johnson <nos...@xxxxxxxxxxxxxxxxxxxxxxx>
wrote:
Patricia Shanahan wrote:
I think this question has already been asked, but I have failed to work
out a good search term to find the previous discussion.

Suppose I have a Set<MyType> mySet and a MyType myObject.
mySet.contains(myObject) is true. I need a reference to the equal object
that is already in mySet.

Currently, I do this by maintaining a Map<MyType,MyType>. When the get
call returns null I do a myMap.put(myObject, myObject), so that future
get calls with equal objects will return the actual reference.

Is there a better way to handle this? Any way to make a Set return a
reference to the object it contains?

Patricia

How does .contains() find the Object? I would guess that in an
unordered Set it must iterate.

Isn't kinda the whole point of a set/map to use hashes
to have constant time for such operations?

For example, from the Javadoc for HashSet (which states
that it makes no guarantees regarding as to the iteration
order over the set -- which I then deduce match your
definition of an unordered set):

* This class offers constant time performance for the
* basic operations add, remove, contains and size,
* assuming the hash function disperses the elements
* properly among the buckets.

.



Relevant Pages

  • Re: Get reference to object in Set
    ... out a good search term to find the previous discussion. ... I need a reference to the equal object that is already in mySet. ... When the get call returns null I do a myMap.put, so that future get calls with equal objects will return the actual reference. ... One of the ideas that came up was using a map not from MyType to MyType, ...
    (comp.lang.java.programmer)
  • Re: Get reference to object in Set
    ... work out a good search term to find the previous discussion. ... I need a reference to the equal object that is already in mySet. ... When the get call returns null I do a myMap.put, so that future get calls with equal objects will return the actual reference. ... One of the ideas that came up was using a map not from MyType to MyType, ...
    (comp.lang.java.programmer)
  • Re: Get reference to object in Set
    ... Suppose I have a SetmySet and a MyType myObject. ... I need a reference to the equal object ... get calls with equal objects will return the actual reference. ...
    (comp.lang.java.programmer)
  • Get reference to object in Set
    ... Suppose I have a SetmySet and a MyType myObject. ... I need a reference to the equal object ... get calls with equal objects will return the actual reference. ...
    (comp.lang.java.programmer)