-1

When will equal() and hashcode() methods of AbstractSet get called? What is the need of those methods in AbstractSet?

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Nani
  • 49
  • 1
  • 6
  • Because that is why they exist. Being called is the sole purpose of a method. – Rohit Jain Apr 15 '15 at 14:29
  • 1
    When they need to be called. – Alexis C. Apr 15 '15 at 14:29
  • you'll have to be a bit more specific, if you want an answer. –  Apr 15 '15 at 14:30
  • I guess we can't answer your question; because it doesn't seem to make sense. Maybe you should explain **why** you think you need to know that ... that could help us to resolve your "real" problem. – GhostCat Apr 15 '15 at 14:37
  • what is the need of those methods in AbstractSet? – Nani Apr 15 '15 at 14:41
  • since we use both equals and hashcode methods in hashing algorithms , wrapper classes already consisting of these methods. What is the need of defining those methods in AbstractSet? Does any of the collection class uses these methods? – Nani Apr 15 '15 at 14:51

2 Answers2

0

AbstractSet's equals and hashcode methods will get inherited by any subclass of AbstractSet that doesn't override them. They would get called when using that set as an element in a collection or map.

The methods use the contained elements to determine whether this set is equal to another. The JDK subclasses EnumSet, HashSet, and TreeSet use these implementations of equals and hashCode, the API documentation lists them under Methods inherited from class java.util.AbstractSet.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • http://stackoverflow.com/questions/3341202/what-is-the-fastest-way-to-compare-two-sets-in-java – Nani Apr 16 '15 at 02:44
0

I found the answer for my question for hashcode() by trying the sample example. hashcode() method will get called when you add set elements in hashmap as key eg:

Set<String> set = new HashSet<String>();
set.add("A");
set.add("B");
Map<Set<String>,String> m = new HashMap<Set<String>,String>();
m.put(set,"B");

put debug point in hashcode() method of AbstractSet it will get called.

Nani
  • 49
  • 1
  • 6