banner



How To Compare Two Set Values In Java

This example shows how to compare 2 HashSet objects to cheque if they are equal in Java. This example also shows how to compare HashSet objects using the equals method.

How to compare two HashSet objects for equality in Coffee?

The HashSet class inherits the equals method from the parent class AbstractSet class.

This method compares the specified object with this Set object and returns true if the specified object is a Ready, both have the same number of elements (HashSet size), and every chemical element of this Set is likewise contained in the specified Set object.

Let's have a look at the source lawmaking of the equals method taken from the OpenJDK eight AbstractSet form.

As nosotros can meet from the code, there are some operation shorts cuts practical before comparing the elements of ii Set objects. First of all, it checks if the two references point to the same object, if yes, information technology returns true. Then, if the specified object is not an case of a Set, it returns fake. In one case that check is done, it compares the size of 2 Set objects. If the size is not the aforementioned, the Set objects are not equal.

If sizes are equal for both the Set objects, it checks if all the elements of this Ready are also contained in the specified Set object. If yes, information technology returns true, false otherwise.

In short, the equals method compares two Set objects by elements i.e. the bodily content of the Ready, not merely the references. The below example shows how to use the equals method to compare 2 HashSet objects.

Output

How to compare two HashSet objects of custom class elements?

Permit's see what happens when we compare the set objects having custom class elements using the equals method.

Output

As we tin come across from the output, even if the two HashSet objects were having the same custom course objects, the equals method returned false.

As we have seen in the source lawmaking of the HashSet equals method given at the elevation, the equals method checks if all the elements of this Set object are also contained in the specified Set object using the containsAll method. The containsAll method relies on the equals method of the elements to compare the objects.

Since we have not implemented the equals method in our Document custom class, the equals method inherited from the Object class is used which simply compares the object references, not the bodily content. That is the reason the containsAll returns false and in turn the HashSet equals method returns false.

Allow's implement the equals and hashCode methods in the Document class and endeavour again.

Output

It worked this time. E'er remember to override the equals and hashCode methods in your custom class for the HashSet or any other Gear up comparisons to work properly.

This example is a role of the HashSet in Java Tutorial with Examples.

Delight let me know your views in the comments section below.

References:
Java 8 HashSet

About the author

RahimV

My proper name is RahimV and I take over xvi years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality just unproblematic to empathize Java tutorials and examples for complimentary. If you like my website, follow me on Facebook and Twitter.

How To Compare Two Set Values In Java,

Source: https://www.javacodeexamples.com/compare-two-hashset-objects-in-java-example-equals/2781

Posted by: freelsject1971.blogspot.com

0 Response to "How To Compare Two Set Values In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel