Sets and Maps in Java
Sets and Maps in Java
Example:
Output
[Set, Example, Geeks, For]
Operations on the Set Interface
Let set1 = [1, 3, 2, 4, 8, 9, 0] and set2 = [1, 3, 7, 5, 4, 0, 7, 5].
hs.remove("B");
// Printing Set elements after removing an element
// and printing updated Set elements
System.out.println("After removing element " + hs);
Iterating through the Set
HashSet Size
cars.size();
Loop Through a HashSet
for (String i : cars) {
System.out.println(i);
}
The list interface allows Set does not allow The map does not allow duplicate
duplicate elements duplicate elements. elements
The list maintains Set do not maintain any The map also does not maintain any
insertion order. insertion order. insertion order.
We can add any number of But in set almost only The map allows a single null key at
null values. one null value. most and any number of null values.