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.
| public boolean equals ( Object o ) |
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.
| 1 2 3 iv five half-dozen 7 8 9 ten eleven 12 xiii 14 15 sixteen 17 | public boolean equals ( Object o ) { if ( o == this ) return true ; if ( ! ( o instanceof Set ) ) return imitation ; Collection <? > c = ( Collection <? > ) o ; if ( c . size ( ) != size ( ) ) return false ; try { return containsAll ( c ) ; } catch ( ClassCastException unused ) { return faux ; } catch ( NullPointerException unused ) { return false ; } } |
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.
| ane 2 iii four 5 6 7 8 nine 10 xi 12 thirteen 14 15 sixteen 17 18 19 20 21 22 23 24 25 26 27 28 | import java . util . HashSet ; public class JavaHashSetCompareExample { public static void chief ( String [ ] args ) { //starting time HashSet HashSet <Integer> hSet1 = new HashSet <Integer> ( ) ; hSet1 . add ( 1 ) ; hSet1 . add ( 2 ) ; hSet1 . add ( 3 ) ; //2nd HashSet HashSet <Integer> hSet2 = new HashSet <Integer> ( ) ; hSet2 . add ( 3 ) ; hSet2 . add ( 2 ) ; hSet2 . add ( 1 ) ; Organisation . out . println ( "Equal? " + hSet1 . equals ( hSet2 ) ) ; //remove an element from 1 HashSet hSet1 . remove ( two ) ; Organization . out . println ( "Equal? " + hSet1 . equals ( hSet2 ) ) ; } } |
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.
| 1 2 three 4 v 6 7 viii ix 10 xi 12 13 14 fifteen sixteen 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import java . util . HashSet ; class Certificate { individual int documentId ; public Document ( int documentId ) { this . documentId = documentId ; } public String toString ( ) { return "Document: " + documentId ; } } public class JavaHashSetCompareExample { public static void main ( String [ ] args ) { HashSet <Document> hSetDocuments1 = new HashSet <Document> ( ) ; hSetDocuments1 . add together ( new Document ( one ) ) ; hSetDocuments1 . add ( new Certificate ( 2 ) ) ; hSetDocuments1 . add ( new Document ( three ) ) ; HashSet <Document> hSetDocuments2 = new HashSet <Document> ( ) ; hSetDocuments2 . add ( new Document ( one ) ) ; hSetDocuments2 . add ( new Document ( 2 ) ) ; hSetDocuments2 . add ( new Document ( 3 ) ) ; Organisation . out . println ( "Equals? " + hSetDocuments1 . equals ( hSetDocuments2 ) ) ; } } |
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.
| 1 2 3 4 5 6 vii eight ix 10 11 12 13 14 15 sixteen 17 xviii 19 xx 21 22 23 24 25 26 27 28 29 xxx 31 32 33 34 35 36 37 38 39 forty 41 42 43 44 45 46 47 48 49 l 51 52 53 54 | import java . util . HashSet ; class Document { private int documentId ; public Document ( int documentId ) { this . documentId = documentId ; } public Cord toString ( ) { return "Document: " + documentId ; } public int hashCode ( ) { final int prime = 31 ; int result = ane ; event = prime * result + documentId ; return result ; } public boolean equals ( Object obj ) { if ( this == obj ) return true ; if ( obj == null ) return faux ; if ( getClass ( ) != obj . getClass ( ) ) return false ; Document other = ( Certificate ) obj ; if ( documentId != other . documentId ) render false ; return true ; } } public class JavaHashSetCompareExample { public static void main ( String [ ] args ) { HashSet <Document> hSetDocuments1 = new HashSet <Document> ( ) ; hSetDocuments1 . add ( new Document ( one ) ) ; hSetDocuments1 . add ( new Document ( 2 ) ) ; hSetDocuments1 . add ( new Certificate ( 3 ) ) ; HashSet <Certificate> hSetDocuments2 = new HashSet <Document> ( ) ; hSetDocuments2 . add ( new Document ( 1 ) ) ; hSetDocuments2 . add together ( new Certificate ( 2 ) ) ; hSetDocuments2 . add ( new Certificate ( three ) ) ; System . out . println ( "Equals? " + hSetDocuments1 . equals ( hSetDocuments2 ) ) ; } } |
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
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