Java का LinkedHashSet
In this page:
LinkedHashSet<Type> set = new LinkedHashSet<>();
set.add(item); // keeps insertion order
Introduction to LinkedHashSet
LinkedHashSet Set का एक implementation है जो एक hash table को एक internal doubly-linked list के साथ combine करता है, इसे HashSet की fast lookup speed और एक predictable iteration order दोनों देते हुए।
उदाहरण: Introduction to LinkedHashSet
import java.util.LinkedHashSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<String> set = new LinkedHashSet<>(); // hash table + linked list
set.add("a");
System.out.println(set);
}
}
Login to try C/C++/Java/PHP code in the editor
Insertion Order Preserved
HashSet के विपरीत, LinkedHashSet हमेशा अपने elements को बिल्कुल उस order में iterate करता है जिसमें वे originally insert हुए थे, और पहले से मौजूद एक element फिर जोड़ना उस order में इसकी position नहीं बदलता।
उदाहरण: Insertion Order Preserved
import java.util.LinkedHashSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("banana");
set.add("apple");
set.add("banana"); // re-adding doesn't move position
System.out.println(set);
}
}
Login to try C/C++/Java/PHP code in the editor
LinkedHashSet vs HashSet
LinkedHashSet add, remove, और contains के लिए HashSet जैसी ही average-case performance प्रदान करता है, linked list maintain करने के लिए थोड़ी ज़्यादा memory की कीमत पर, एक guaranteed, predictable iteration order के बदले।
उदाहरण: LinkedHashSet vs HashSet
import java.util.LinkedHashSet;
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> hash = new HashSet<>();
LinkedHashSet<String> linked = new LinkedHashSet<>();
hash.add("z"); hash.add("a");
linked.add("z"); linked.add("a");
System.out.println(linked); // predictable order
}
}
Login to try C/C++/Java/PHP code in the editor
LinkedHashSet vs TreeSet
LinkedHashSet TreeSet से इसमें अलग है कि यह कौन सा order maintain करता है: LinkedHashSet elements insert होने का order संरक्षित रखता है, जबकि TreeSet हमेशा elements को उनकी natural ordering या एक supplied Comparator के अनुसार sorted रखता है।
उदाहरण: LinkedHashSet vs TreeSet
import java.util.LinkedHashSet;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<String> linked = new LinkedHashSet<>();
TreeSet<String> tree = new TreeSet<>();
linked.add("banana"); linked.add("apple");
tree.add("banana"); tree.add("apple");
System.out.println(linked); // insertion order
System.out.println(tree); // sorted order
}
}
Login to try C/C++/Java/PHP code in the editor
When to Use LinkedHashSet
LinkedHashSet सही choice है जब भी duplicates हटाने हों लेकिन जिस original order में वे दिखे वह भी मायने रखता हो, जैसे एक user के unique recent searches track करना या एक list को deduplicate करते हुए इसकी sequence संरक्षित रखना।
उदाहरण: When to Use LinkedHashSet
import java.util.LinkedHashSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<String> recentSearches = new LinkedHashSet<>();
recentSearches.add("java");
recentSearches.add("java"); // deduplicated
recentSearches.add("python");
System.out.println(recentSearches);
}
}
Login to try C/C++/Java/PHP code in the editor
- Sorted order की उम्मीद करना, जब यह insertion order रखता है।
- Index access की उम्मीद करना, जो sets के पास नहीं।
- इसे उपयोग करना जब order मायने नहीं रखता, जहां
HashSetlighter है।
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