Java HashSet
Create a HashSet<String> named tags, call tags.add("oak");, then tags.add("pine");, then tags.add("oak"); again, and print tags.size(). Compile Bin.java and run java Bin. The screen should show one line: 2. The second oak did not create a third value. Unique values in one set are what this page teaches.
Unique values, not key-and-value pairs
On the HashMap page you stored a key that pointed to a value with put and get. A HashSet stores values alone. Each value appears at most once. If you add the same word twice, the set still holds it once. There is no separate key string on this teaching path.
Open Bin.java. Keep the public class name Bin so the file name matches. Delete leftover HashMap lines, scores.put, scores.get, and any Desk demo. Those belong to the map page. Also delete any ArrayList, Rack, Scanner, or nextLine leftover. The program must not wait for typing.
Near the top, above public class Bin, write:
import java.util.HashSet;
That import brings the HashSet type into short-name reach. Without it, javac cannot see the short name HashSet on this page. Spell java.util and HashSet carefully. Do not invent import java.util.*; as a requirement here — one exact import is enough.
Inside main, create the set:
HashSet<String> tags = new HashSet<String>();
The angle brackets hold String because this lesson stores words. The name tags is the name you keep. The new HashSet<String>() call builds an empty set. At this moment it holds zero values.
Add oak, pine, oak again — then size
Call tags.add("oak"); on its own line. Then call tags.add("pine");. Then call tags.add("oak"); a third time with the same word. The third call does not grow the set. Then write:
System.out.println(tags.size());
Print the size, not the whole set, for the first kept file. The number 2 proves the duplicate was ignored. Save before you compile.
import java.util.HashSet;
public class Bin {
public static void main(String[] args) {
HashSet<String> tags = new HashSet<String>();
tags.add("oak");
tags.add("pine");
tags.add("oak");
System.out.println(tags.size());
}
}
From the folder that holds Bin.java, run javac Bin.java. When parentheses, braces, angle brackets, and semicolons match, javac writes no line of its own. Then run java Bin — not java Bin.java, and not java HashSet. Leave the .java ending off the run command.
The program should print one line and stop: 2. It should not ask you to type. If the terminal waits for keyboard input, a nextLine call from an older exercise is still in a file. Delete that call and keep the create, the three add lines, and System.out.println(tags.size());.
Walk the run once. Java starts at Bin.main. The import has already named which HashSet you mean. The new line builds an empty set. The first add stores oak. The second stores pine. The third add("oak") finds oak already present and does not raise the count. The print of size() writes 2. The screen shows 2. Nothing else should print. If you see 3, you are not running this HashSet file, or you added a different third word such as elm. If you see leaf, a Desk HashMap demo is still running — switch to java Bin. If you see [oak, pine] you printed the whole set instead of size(); keep the size print for this first teaching file.
If javac says it cannot find HashSet, the import line is missing, misspelled, or placed after public class Bin. Move import java.util.HashSet; above the class line. If you write tags.add(oak); without quotes, javac treats oak as a missing name. Keep the quotes. Do not call put on a HashSet on this page — that name belongs to HashMap.
Ask with contains
A second Bin file checks whether oak is in the set after the same first two adds.
import java.util.HashSet;
public class Bin {
public static void main(String[] args) {
HashSet<String> tags = new HashSet<String>();
tags.add("oak");
tags.add("pine");
System.out.println(tags.contains("oak"));
}
}
Compile and run that second file the same way. The screen shows true. The call contains("oak") asks whether that value sits in the set. It returns a boolean. If you ask contains("elm") after only oak and pine were added, the screen would show false — that is valid, but the kept second file uses "oak" so the screen is true.
Do not invent a loop over the set as a requirement on this page. A later page — iterating — walks collections. Stay with add, size, and contains here.
Side by side with HashMap
One short table keeps the difference clear.
📊 HashSet vs HashMap
| Piece | On this page |
|---|---|
| store | tags.add("oak"); — value only |
| duplicate | second add("oak") does not grow size |
| read check | tags.contains("oak") → true |
| HashMap leftover | put / get — not this page’s path |
The table is only a reminder. The typed pieces you keep are the HashSet import, the create line, the add calls, size(), and contains. |
Compared with HashMap and with ArrayList
On the HashMap page, put and get used a key. On this page, add and contains use the value itself. A leftover Desk file that prints leaf is a different lesson. For this page keep HashSet<String> tags and the size demo that prints 2.
An ArrayList can hold the same word twice. A HashSet will not grow for a second identical add of that word. That uniqueness is the point of this lesson. Do not mix tags.get(0) into the Bin demos — sets do not use list slot numbers on this page.
When old Desk.java or Rack.java files still sit nearby, leave them alone or delete them only if they confuse which class you run. Run java Bin after you compile the HashSet file from this lesson.
What this leaves for iterating
This page opens a set of unique values. The next page walks collections with a loop. Stay with add, size, and contains here. Do not invent a for-each over the set as a requirement on this page; the lesson goal is uniqueness and the screen 2 from size().
Before iterating
You can import java.util.HashSet, create HashSet<String> tags, add oak, pine, and oak again, then print tags.size() to see 2. A second file can print tags.contains("oak") to see true. Compile Bin.java and run java Bin for each kept demo. The set kept unique values. The next page is iterating, where you walk the values in a collection one by one.
Keep the import above the class. Keep the duplicate add("oak") in the size demo so the count stays 2. Keep contains for the boolean check. Those habits are the checkpoint for this page before you move on. When size() shows 2 after three adds with a repeated oak, the HashSet did its job for this lesson and you are ready for the next topic.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top