here is my code:
public class testGui { public static void main(String[] arg){ class TESTS{ String t; public TESTS(String t){ this.t = t; } @Override public boolean equals(Object x){ System.out.println("My method is called..."); if(x instanceof TESTS){ TESTS zzz = (TESTS) x; return zzz.t.compareTo(t)==0; } else return false; } } HashSet<TESTS> allItems = new HashSet<TESTS>(); allItems.add(new TESTS("a")); allItems.add(new TESTS("a")); System.out.println(allItems.contains(new TESTS("a"))); } }
I do not understand why hashset contains a method that does not call my equals method, as indicated in their specifications:
More formally adds the indicated element, o, to this set if this set does not contain an element e such that (o == null? E == null: o.equals (e))
My code returns false and is not part of my equals method.
Thank you very much for your answer!
java
Abbadon
source share