Java HashSet contains Object -
i made own class overridden equals method checks, if names (attributes in class) equal. store instances of class in hashset there no instances same names in hashset.
my question: how possible check if hashset contains such object. .contains() wont work in case, because works .equals() method. want check if same object.
edit:
package testprogram; import java.util.hashset; import java.util.set; public class example { private static final set<example> set = new hashset<example>(); private final string name; private int example; public example(string name, int example) { this.name = name; this.example = example; set.add(this); } public boolean isthisinlist() { return set.contains(this); //will return true if equal instance in list //but should not //it should return true if object in list } public boolean remove() { return set.remove(this); } //override equals , hashcode }
sorry, english skills not well. please feel free ask again if don't understand mean.
in situation, way tell if particular instance of object contained in hashset, iterate contents of hashset, , compare object identities ( using == operator instead of equals() method).
something like:
boolean isobjectinset(object object, set<? extends object> set) { boolean result = false; for(object o : set) { if(o == object) { result = true; break; } } return result; }
Comments
Post a Comment