It returns true if the specified Object ‘ob’ has same value as the ‘BooleanObject’, else it returns false. Below are programs to illustrate the equals () method of Boolean class: Program 1: class GeeksforGeeks {. public static void main (String [] args) {. Boolean a = new Boolean (true); Boolean b = new Boolean (true);
99 4. A code-only answer is not high quality. While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please edit your answer to include explanation and link to relevant documentation. – Stephen Ostermiller. 1. As long as your objects implement equals, you can use the Stream API to get a distinct set of values, and if that distinct count is 1, they are all the same. boolean allEqual = Stream.of (obj1, obj2, obj3).distinct ().count () == 1. Share. Improve this answer. Follow. Question 2 : If the answer is because they both have same hash code,only then equals will be called, then why its not called for below code. sset.add (obj1); sset.add (obj4); sset.add (obj2); sset.add (obj4); output is : in hash code value is 98 in hash code value is 97 in hash code value is 99 in hash code value is 97.And just like Pablo said, if you use anything other than the Object class in your equals method signature, you aren't actually overriding the equals method, and your program won't work as expected. Take for example this small program that copies a List to a Set (which cannot contain duplicates) and prints the new Collection.
Zlpay.