Java का HashSet
In this page:
HashSet<Type> set = new HashSet<>();
set.add(item);
set.contains(item);
set.remove(item);
Introduction to HashSet
HashSet internally एक HashMap से backed unique elements का एक collection store करता है, चुपचाप किसी भी element को discard करते हुए जिसे आप जोड़ने की कोशिश करें जो equals() के अनुसार पहले से मौजूद है।
उदाहरण: Introduction to HashSet
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("apple");
set.add("apple"); // silently discarded
System.out.println(set.size());
}
}
Login to try C/C++/Java/PHP code in the editor
Removing and Searching Elements
remove(element) मौजूद होने पर एक matching element delete करता है, और contains(element) membership जांचता है — दोनों HashSet के HashMap से inherited उसी hashing की वजह से average O(1) time में चलते हैं।
उदाहरण: Removing and Searching Elements
// Import java.util.HashSet so it can be used by its short name
import java.util.HashSet;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new HashSet object and store it in set
HashSet<String> set = new HashSet<>();
set.add("apple");
// Print a line to the console
System.out.println(set.contains("apple"));
set.remove("apple");
// Print a line to the console
System.out.println(set.contains("apple"));
}
}
Login to try C/C++/Java/PHP code in the editor
Iterating over HashSet
एक HashSet को iterate करना हर unique element को बिल्कुल एक बार visit करता है, लेकिन HashMap की तरह, यह elements किस order में वापस आते हैं इसकी कोई guarantee नहीं देता — insertion order या किसी particular sequence पर भरोसा न करें।
उदाहरण: Iterating over HashSet
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
for (String item : set) { // order not guaranteed
System.out.println(item);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Set Operations
HashSet अपने collection methods के through mathematical set operations support करता है: union के लिए addAll(), intersection के लिए retainAll(), और दो sets के बीच difference के लिए removeAll()।
उदाहरण: Set Operations
// Import java.util.HashSet so it can be used by its short name
import java.util.HashSet;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new HashSet object and store it in a
HashSet<Integer> a = new HashSet<>(java.util.List.of(1, 2, 3));
// Create a new HashSet object and store it in b
HashSet<Integer> b = new HashSet<>(java.util.List.of(2, 3, 4));
HashSet<Integer> intersection = new HashSet<>(a);
intersection.retainAll(b);
// Print a line to the console
System.out.println(intersection);
}
}
Login to try C/C++/Java/PHP code in the editor
Converting HashSet to List
एक HashSet को एक List में convert करना (new ArrayList<>(mySet) के ज़रिए) उपयोगी है जब आपको set को विशुद्ध रूप से इसकी fast uniqueness guarantee के लिए उपयोग करने के बाद indexed access या एक defined order चाहिए।
उदाहरण: Converting HashSet to List
import java.util.HashSet;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
ArrayList<String> list = new ArrayList<>(set); // now has indexed access
System.out.println(list.get(0));
}
}
Login to try C/C++/Java/PHP code in the editor
- Iterate करते समय एक order की उम्मीद करना, क्योंकि
HashSetका कोई नहीं। - Duplicates की उम्मीद करना, जो नज़रअंदाज़ किए जाते हैं।
equals()औरhashCode()के बिना objects उपयोग करना, इसलिए equal objects दो बार store होते हैं।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting