Java का Set Interface
In this page:
Set<Type> set = new HashSet<>();
set.add(item);
set.contains(item);
What is the Set Interface?
Set interface एक collection represent करता है जिसमें कोई duplicate elements नहीं, और जब भी stored values की uniqueness उनके जोड़े जाने के order से ज़्यादा मायने रखे उपयोग होता है।
उदाहरण: What is the Set Interface?
import java.util.Set;
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
Set<String> names = new HashSet<>();
names.add("Alice");
names.add("Alice"); // duplicate ignored
System.out.println(names.size());
}
}
Login to try C/C++/Java/PHP code in the editor
Set Implementations
HashSet, LinkedHashSet, और TreeSet Set के तीन standard implementations हैं: HashSet कोई ordering guarantee के बिना सबसे तेज़ operations प्रदान करता है, LinkedHashSet insertion order संरक्षित रखता है, और TreeSet elements को sorted रखता है।
उदाहरण: Set Implementations
import java.util.Set;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Set<String> hash = new HashSet<>();
Set<String> linked = new LinkedHashSet<>(); // insertion order
Set<String> tree = new TreeSet<>(); // sorted
hash.add("a"); linked.add("a"); tree.add("a");
System.out.println(hash + " " + linked + " " + tree);
}
}
Login to try C/C++/Java/PHP code in the editor
No Duplicates Allowed
Set का हर implementation पहले से मौजूद एक value जोड़ने की कोशिश को चुपचाप नज़रअंदाज़ करता है -- add method एक error throw करने के बजाय false return करता है, और set का size एक duplicate insertion से कभी नहीं बढ़ता।
उदाहरण: No Duplicates Allowed
import java.util.Set;
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
Set<String> names = new HashSet<>();
System.out.println(names.add("Alice")); // true
System.out.println(names.add("Alice")); // false: already present
}
}
Login to try C/C++/Java/PHP code in the editor
Common Set Methods
Set interface add, remove, contains, और size जैसे methods define करता है जो हर implementation में एक जैसे काम करते हैं, code को HashSet, TreeSet, या LinkedHashSet के बीच switch करने देते हुए बिना यह बदले कि यह set के साथ कैसे interact करता है।
उदाहरण: Common Set Methods
// Import java.util.Set so it can be used by its short name
import java.util.Set;
// 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 names
Set<String> names = new HashSet<>();
names.add("Alice");
// Print a line to the console
System.out.println(names.contains("Alice"));
// Print a line to the console
System.out.println(names.size());
names.remove("Alice");
System.out.println(names.contains("Alice"));
}
}
Login to try C/C++/Java/PHP code in the editor
Choosing a Set Implementation
Set implementations के बीच चुनना इस पर आ जाता है कि आपको कौन सी guarantee चाहिए: fastest general-purpose set के लिए HashSet, जब insertion order याद रखना चाहिए तो LinkedHashSet, और जब elements को हमेशा sorted रहना चाहिए तो TreeSet।
उदाहरण: Choosing a Set Implementation
import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Set<Integer> fast = new HashSet<>(); // fastest, no order
Set<Integer> sorted = new TreeSet<>(); // sorted order
fast.add(3);
sorted.add(3);
System.out.println(fast + " " + sorted);
}
}
Login to try C/C++/Java/PHP code in the editor
- एक set से insertion order रखने की उम्मीद करना, जो
HashSetनहीं करता। - Index से एक element पाने की कोशिश करना, जो sets support नहीं करते।
- Proper
equals()औरhashCode()के बिना objects जोड़ना, इसलिए duplicates detect नहीं होते।
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