
上QQ阅读APP看书,第一时间看更新
About equals ()
The equals() method returns true when the passed object is considered the same as the object that the caller of the method used, otherwise it returns false. A simple example is as follows:
Integer i = 25;
Object o = new Object();
System.out.println(i.equals(o));
The example above will print false to the console.
The equals() method is expected to match both objects and return whether the objects are similar. There are many rules that this method has to adhere to. Again, these are explained in the Java chapter, as typically Java developers will have to write these methods themselves.
If a class does not provide an implementation that would follow all the rules (also called contract) of the equals() method, then the correct working of the Collection classes cannot be guaranteed.