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

Java का HashSet

एक HashSet बिना repeats वाले unique items का एक bag है, अलग-अलग stickers के एक collection जैसा। एक duplicate जोड़ना कुछ नहीं करता।
Syntax
java
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

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

Removing and Searching Elements

remove(element) मौजूद होने पर एक matching element delete करता है, और contains(element) membership जांचता है — दोनों HashSet के HashMap से inherited उसी hashing की वजह से average O(1) time में चलते हैं।

उदाहरण: Removing and Searching Elements

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

Iterating over HashSet

एक HashSet को iterate करना हर unique element को बिल्कुल एक बार visit करता है, लेकिन HashMap की तरह, यह elements किस order में वापस आते हैं इसकी कोई guarantee नहीं देता — insertion order या किसी particular sequence पर भरोसा न करें।

उदाहरण: Iterating over HashSet

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

Set Operations

HashSet अपने collection methods के through mathematical set operations support करता है: union के लिए addAll(), intersection के लिए retainAll(), और दो sets के बीच difference के लिए removeAll()।

उदाहरण: Set Operations

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

Converting HashSet to List

एक HashSet को एक List में convert करना (new ArrayList<>(mySet) के ज़रिए) उपयोगी है जब आपको set को विशुद्ध रूप से इसकी fast uniqueness guarantee के लिए उपयोग करने के बाद indexed access या एक defined order चाहिए।

उदाहरण: Converting HashSet to List

java
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));
	}
}
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. Iterate करते समय एक order की उम्मीद करना, क्योंकि HashSet का कोई नहीं।
  2. Duplicates की उम्मीद करना, जो नज़रअंदाज़ किए जाते हैं।
  3. equals() और hashCode() के बिना objects उपयोग करना, इसलिए equal objects दो बार store होते हैं।

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.