Feb 1, 2025
Collections in Java
Child Interfaces
Set Interface Overview
Characteristics of Set Interface
HashSet Class
hashCode
for element storage, enhancing search speed.Key Features of HashSet
Default Creation
HashSet hs = new HashSet();
// Default initial size: 16, Load factor: 0.75
Custom Initial Size and Load Factor
HashSet hs = new HashSet(100);
HashSet hs = new HashSet(100, 0.90f);
Type Restriction
HashSet<Integer> hs = new HashSet<>();
// Stores only Integer objects
Adding Elements
hs.add(value);
hs.addAll(collection);
Removing Elements
hs.remove(value);
hs.removeAll(collection);
Checking Elements
hs.contains(value);
hs.containsAll(collection);
hs.isEmpty();
Iteration
for (Object obj : hs) { ... }
Iterator it = hs.iterator();
while (it.hasNext()) { ... }
ArrayList
.Union, Intersection, Difference
addAll()
, retainAll()
, removeAll()
to perform set operations.Subset Checking
hs.containsAll(otherSet);