public
boolean equals(Object o)
Compare
the given object with the set for equality. Returns true if the given
object is also a set and the two sets has same elements.
import java.util.*; class HashSetEquals{ public static void main(String args[]){ HashSet<Integer> mySet1 = new HashSet<> (); TreeSet<Integer> mySet2 = new TreeSet<> (); HashSet<Integer> mySet3 = new HashSet<> (); ArrayList<Integer> myList1 = new ArrayList<> (); for(int i=0; i < 5; i++){ mySet1.add(i); mySet2.add(i); myList1.add(i); } mySet3.add(10); mySet3.add(20); System.out.print("\nElements in mySet1 "); System.out.println(mySet1); System.out.print("\nElements in mySet2 "); System.out.println(mySet2); System.out.print("\nElements in mySet3 "); System.out.println(mySet3); System.out.print("\nElements in myList1 "); System.out.println(myList1); System.out.print("\nIs mySet1 and mySet2 are equal "); System.out.println(mySet1.equals(mySet2)); System.out.print("\nIs mySet1 and mySet3 are equal "); System.out.println(mySet1.equals(mySet3)); System.out.print("\nIs mySet1 and myList1 are equal "); System.out.println(mySet1.equals(myList1)); } }
Output
Elements in mySet1 [0, 1, 2, 3, 4] Elements in mySet2 [0, 1, 2, 3, 4] Elements in mySet3 [20, 10] Elements in myList1 [0, 1, 2, 3, 4] Is mySet1 and mySet2 are equal true Is mySet1 and mySet3 are equal false Is mySet1 and myList1 are equal false
No comments:
Post a Comment