← Back to Core Java Course | Chapter 11: Collections | Lesson 13 of 17

Java का LinkedHashSet

LinkedHashSet एक hash table को एक internal linked list के साथ combine करता है, इसे HashSet की fast lookups के साथ एक predictable, insertion-ordered iteration देते हुए।
Syntax
java
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

java
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);
	}
}

Insertion Order Preserved

HashSet के विपरीत, LinkedHashSet हमेशा अपने elements को बिल्कुल उस order में iterate करता है जिसमें वे originally insert हुए थे, और पहले से मौजूद एक element फिर जोड़ना उस order में इसकी position नहीं बदलता।

उदाहरण: Insertion Order Preserved

java
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);
	}
}

LinkedHashSet vs HashSet

LinkedHashSet add, remove, और contains के लिए HashSet जैसी ही average-case performance प्रदान करता है, linked list maintain करने के लिए थोड़ी ज़्यादा memory की कीमत पर, एक guaranteed, predictable iteration order के बदले।

उदाहरण: LinkedHashSet vs HashSet

java
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
	}
}

LinkedHashSet vs TreeSet

LinkedHashSet TreeSet से इसमें अलग है कि यह कौन सा order maintain करता है: LinkedHashSet elements insert होने का order संरक्षित रखता है, जबकि TreeSet हमेशा elements को उनकी natural ordering या एक supplied Comparator के अनुसार sorted रखता है।

उदाहरण: LinkedHashSet vs TreeSet

java
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
	}
}

When to Use LinkedHashSet

LinkedHashSet सही choice है जब भी duplicates हटाने हों लेकिन जिस original order में वे दिखे वह भी मायने रखता हो, जैसे एक user के unique recent searches track करना या एक list को deduplicate करते हुए इसकी sequence संरक्षित रखना।

उदाहरण: When to Use LinkedHashSet

java
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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Sorted order की उम्मीद करना, जब यह insertion order रखता है।
  2. Index access की उम्मीद करना, जो sets के पास नहीं।
  3. इसे उपयोग करना जब order मायने नहीं रखता, जहां HashSet lighter है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.