[!NOTE] Sometimes you don't care about order, and you don't even care about key-value mapping. You just have a massive influx of data and you need to mathematically guarantee that every single item is completely unique.
If you use an ArrayList, adding "BMW" ten times will add ten distinct "BMW" entries into the list.
If you use a HashSet, it evaluates the data, realizes "BMW" already exists, and silently ignores the other nine additions.
Use Cases
Imagine you are parsing a text file containing 10,000 email addresses, trying to build a spam-marketing list. If John signed up 5 times, you don't want to email him 5 times. By dumping the entire list into a HashSet, all duplicates are automatically stripped away in milliseconds!
Syntax
The syntax is identical to an ArrayList, but with the magical uniqueness guarantee natively built-in.
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> cars = new HashSet<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("BMW"); // Ignored!
cars.add("Mazda");
cars.add("Volvo"); // Ignored!
// Outputs 4, not 6!
System.out.println("Total Unique Cars: " + cars.size());
// HashSets use .contains() which is incredibly fast!
if(cars.contains("Mazda")) {
System.out.println("Mazda is in the unique Set.");
}
}
}
[!CAUTION] No Concept of Indexing! A
HashSetdoes not use indices. You cannot ask a HashSet for.get(2). The items are mathematically scattered in memory, and the "order" they are stored in is basically random. If you iterate through a HashSet, do not expect it to print in the order you added the items!