Native lesson simulator
Hash map lookup
Hash the key, jump to a bucket, then compare entries there.
Ada hashes to bucket 2; lookup only scans entries in that bucket.
Flash cards
Review the key moves
What is the main idea behind Java Set?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
Which statement best captures the main point of this lesson?
Put the learning moves in the order that makes the concept easiest to apply.
Java Set Interface
The Set interface is part of the Java Collections Framework and is used to store a collection of unique elements .
Unlike a List , a Set does not allow duplicates , and it does not preserve the order of elements (unless you're using TreeSet or LinkedHashSet ).
Common classes that implement Set
- HashSet - fast and unordered
- TreeSet - sorted set
- LinkedHashSet - ordered by insertion
Tip
Use a Set when you need to store unique values only.
Common Set Methods
| Method | Description |
|---|---|
| add() | Adds an element if it's not already in the set |
| remove() | Removes the element from the set |
| contains() | Checks if the set contains the element |
| size() | Returns the number of elements |
| clear() | Removes all elements |
Set vs. List
| List | Set |
|---|---|
| Allows duplicates | Does not allow duplicates |
| Maintains order | Does not guarantee order |
| Access by index | No index-based access |