Java HashSet
Keep unique values. Leave order unspecified.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind HashSet.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
A Set models membership without duplicates. HashSet uses equality and hashing to determine membership, and does not guarantee an iteration order. For custom key objects, equals and hashCode must follow a consistent contract.
விரிவான விளக்கங்கள்
Java HashSet என்னன்னு தெரியுமா?
HashSet-ல duplicate values இல்ல — unique values மட்டுமே store ஆகும். Order guarantee இல்ல.
HashSet use பண்றதுக்கு:
HashSet Methods
add()— element add பண்றதுக்கு (duplicate ignore ஆகும்)contains()— element இருக்கான்னு check பண்றதுக்குremove()— element remove பண்றதுக்குsize()— element countclear()— எல்லாத்தையும் remove பண்றதுக்கு
HashSet<String> cars = new HashSet<>();
cars.add("Volvo");
cars.add("BMW");
cars.add("BMW"); // duplicate — ignored
System.out.println(cars.size()); // 2
System.out.println(cars.contains("BMW")); // true
cars.remove("Volvo");
for (String c : cars) {
System.out.println(c);
}`HashSet` duplicate values-ஐ எப்படி handle செய்யும்?
- Exception throw செய்யும்
- Duplicate-ஐ silently ignore செய்யும்
- Duplicate-ஐ replace செய்யும்
- Both values-ஐயும் வைத்திருக்கும்
Reveal answer / விடை
Duplicate-ஐ silently ignore செய்யும்
`HashSet` duplicate add செய்தால் silently ignore செய்யும் — no exception, no error. `add()` method false return செய்யும். Uniqueness enforce செய்ய `HashSet` ideal.
முழுமையான உதாரணம்
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
import java.util.HashSet;
public class Main {
public static void main(String[] args) throws Exception {
HashSet<String> topics = new HashSet<>();
System.out.println(topics.add("Java"));
System.out.println(topics.add("Java"));
System.out.println(topics.size());
System.out.println(topics.contains("Java"));
}
}Predict first. Then reveal the expected output / வெளியீடு
true false 1 true
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
நீங்களே முயற்சி செய்யுங்கள்
Practice challenge
Add java with a lowercase j. Does the size change?
Reveal the answer and reasoning / விடை
Yes. Java and java are different String values, so the set now contains two elements.
தவிர்க்க வேண்டிய தவறு: Do not test HashSet iteration against a fixed order. Use an ordered collection if order is part of the requirement.
பாடத்தின் சுருக்கம்
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.