jabela

Untitled

Sep 18th, 2024 (edited)
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. import java.util.HashSet;
  2. public class HashSetMain {
  3. public static void main(String[] args) {
  4.  
  5. // Create a new hash set of integers
  6.  
  7. HashSet <Integer> myHash = new HashSet <Integer>();
  8.  
  9. myHash.add(1);
  10. myHash.add(2);
  11. myHash.add(3);
  12. myHash.add(4);
  13. myHash.add(5);
  14.  
  15. HashSet<Integer> myHash2 = new HashSet<Integer>();
  16.  
  17. myHash2.add(4);
  18. myHash2.add(5);
  19. myHash2.add(6);
  20. myHash2.add(7);
  21. myHash2.add(8);
  22.  
  23. // code for union of the hashsets
  24. myHash.addAll(myHash2);
  25. System.out.println("Union of hash set 1 and 2: " + myHash);
  26.  
  27. // code for intersection of hashsets
  28. myHash.retainAll(myHash2);
  29. System.out.println("Intersection of hash set 1 and 2: " + myHash);
  30.  
  31. // code to find difference between two hashsets
  32. myHash.removeAll(myHash2);
  33. System.out.println("Difference of hash set 1 and 2: " + myHash);
  34.  
  35. // code to find if myHash2 is a subset of myHash
  36. boolean subset = myHash.containsAll(myHash2);
  37. System.out.println("myHash2 is a subset of myHash? " + subset);
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment