java - Find an a specific instance, what is the best approach -
so imagine have 2 instances of class:
public class myclass { public void sayhello() { system.out.println("hello"); } } = new myclass(); b = new myclass();
now add object, such as:
public class otherclass { private arraylist<myclass> myclslist = new arraylist<>(); public void add(myclass obj) { myclslist.add(obj); } public void remove(myclass obj) { // ???? } } c = new otherclass(); c.add(a); c.add(b);
now want remove 1 specific instance e.g
c.remove(a);
could iterate on them , test equality, mean should theoretically work, since 2 instances have distinct "internal pointers"?
i guess using hashmap based approach more efficient, can use key there (suppose can't add unique instance ids or something).
edit: there confusion i'd know. key here i'd know if there way of removing specific instance c's arraylist or whatever aggregator object might use, providing respective object reference. imagine done keeping arraylist , testing equality (although i'm not 100% sure) cleaner if possible without iterating through whole list. i'd know if of possible in java. (i know how workaround using additional information clue have respective object reference filtering/ retrieving purposes.
while question 1 many beginners have (including myself), believe concern not justified in case. features asking built java language @ specification level.
first of all, let's @ object.equals()
. on 1 hand, language specification states
the method equals defines notion of object equality, based on value, not reference, comparison.
however, documentation object.equals()
states that
the
equals
method classobject
implements discriminating possible equivalence relation on objects; is, non-null reference valuesx
,y
, method returnstrue
if , ifx
,y
refer same object (x == y
has valuetrue
).
this means can safely redirect otherclass.remove
arraylist.remove()
. whatever object.equals
comparing works unique id. in fact, in many (but not all) implementations, compares memory addresses objects, form of unique id.
quite understandably, not wish use linear iteration every time. happens, machinery of object
suited use hashset, which, way solution recommend use in case.
if not dealing huge data set, not need discuss optimization of object.hashcode()
. need know implement whatever contract necessary work correctly object.equals
make hashset.remove
work correctly.
the spec itself states that
the method
hashcode
useful, methodequals
, in hashtables suchjava.util.hashmap
.
this not much, turn api reference. 2 relevant point are:
- if 2 objects equal according equals(object) method, calling hashcode method on each of 2 objects must produce same integer result.
- it not required if 2 objects unequal according
equals
(java.lang.object
) method, callinghashcode
method on each of 2 objects must produce distinct integer results. however, programmer should aware producing distinct integer results unequal objects may improve performance of hash tables.
simply put, hashcode
of equal objects must same, equal hashcode
not mean equal objects. object
implements contract, can use hashset
, backed hashmap
.
the 1 piece of information missing make formal argument in favor of not doing additional work, why keep citing api reference if language specification. happens:
as noted above, specification refers classes of java se platform api. in particular, classes have special relationship java programming language. examples include classes such
object
,class
,classloader
,string
,thread
, , classes , interfaces in packagejava.lang.reflect
, among others. this specification constrains behavior of such classes , interfaces, not provide complete specification them. reader referred java se platform api documentation.
[emphasis mine], idea. java se api reference is language spec far behavior of methods of object
concerned.
as aside, want stay away treeset
, because will require add bunch of machinery implementation. minimum, myclass
instances have orderable, either implementing comparable
, or assigning custom comparator
set
.
tl;dr
the language specification states have @ least following 2 options available no additional effort on part:
- make
myclslist
arraylist
, use appropriateadd()
/remove()
methods see fit. - make
myclslist
hashset
, use appropriateadd()
/remove()
methods.
i recommend second option. in fact, instead of containment, may consider extending hashset
don't have bother implementing own add
/remove
methods.
final note
all works long myclass
overrides neither object.equals
nor object.hashcode
. moment that, put burden of satisfying contractual requirements entirely on yourself.
Comments
Post a Comment