java - Find duplicates in ArrayList -


could tell me following line in code provided tutor supposed doing?

if (data.contains(entry)) 

full method:

public string add(entry entry) {     if (entry == null)       return "error: null entry";     if (data.contains(entry))       return "error: entry in book";      boolean done = data.add(entry);         if (done)       return " entry added";     else       return "entry not added";   } 

i thought checking existing entry same details within arraylist when add duplicate entry doesnt go if statement figured assumed wrong? searched around on internet , says it's purpose why doesn't work me?

remember contains() performs equality check equals(). unless object overrides method, not consider objects equal unless same object reference.

of course, don't override equals() without overriding hashcode() too.

finally, if don't want duplicate objects consider using set.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -