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); 
  1. could iterate on them , test equality, mean should theoretically work, since 2 instances have distinct "internal pointers"?

  2. 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 class object implements discriminating possible equivalence relation on objects; is, non-null reference values x , y, method returns true if , if x , y refer same object (x == y has value true).

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, method equals, in hashtables such java.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, calling hashcode 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 package java.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:

  1. make myclslist arraylist , use appropriate add()/remove() methods see fit.
  2. make myclslist hashset , use appropriate add()/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

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -