CC104-Data-Structure-and-Algorithms-Week-3-Example-Codes
CC104-Data-Structure-and-Algorithms-Week-3-Example-Codes
1. Sets in Java
The HashSet class in Java is part of java.util and is used to store unique elements.
Method Description
————————————————————————————————————
import java.util.HashSet;
// Union
HashSet<Integer> unionSet = new HashSet<>(setA);
unionSet.addAll(setB);
System.out.println("Union: " + unionSet);
// Intersection
HashSet<Integer> intersectionSet = new HashSet<>(setA);
intersectionSet.retainAll(setB);
System.out.println("Intersection: " + intersectionSet);
// Difference
HashSet<Integer> differenceSet = new HashSet<>(setA);
differenceSet.removeAll(setB);
System.out.println("Difference: " + differenceSet);
}
}
————————————————————————————————————
2. Relations in Java
Method Description
remove(Object key) Removes the key-value pair for the given key.
import java.util.HashMap;
import java.util.HashSet;
————————————————————————————————————
Method Description
import java.util.Arrays;
import java.util.Arrays;
————————————————————————————————————
Method Description
————————————————————————————————————
————————————————————————————————————
————————————————————————————————————
————————————————————————————————————
————————————————————————————————————
————————————————————————————————————