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

Java का Set Interface

Set interface एक collection represent करता है जिसमें कोई duplicate elements नहीं, जब भी stored values की uniqueness उनके जोड़े जाने के order से ज़्यादा मायने रखे उपयोग होता है।
Syntax
java
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?

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

Set Implementations

HashSet, LinkedHashSet, और TreeSet Set के तीन standard implementations हैं: HashSet कोई ordering guarantee के बिना सबसे तेज़ operations प्रदान करता है, LinkedHashSet insertion order संरक्षित रखता है, और TreeSet elements को sorted रखता है।

उदाहरण: Set Implementations

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

No Duplicates Allowed

Set का हर implementation पहले से मौजूद एक value जोड़ने की कोशिश को चुपचाप नज़रअंदाज़ करता है -- add method एक error throw करने के बजाय false return करता है, और set का size एक duplicate insertion से कभी नहीं बढ़ता।

उदाहरण: No Duplicates Allowed

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

Common Set Methods

Set interface add, remove, contains, और size जैसे methods define करता है जो हर implementation में एक जैसे काम करते हैं, code को HashSet, TreeSet, या LinkedHashSet के बीच switch करने देते हुए बिना यह बदले कि यह set के साथ कैसे interact करता है।

उदाहरण: Common Set Methods

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

Choosing a Set Implementation

Set implementations के बीच चुनना इस पर आ जाता है कि आपको कौन सी guarantee चाहिए: fastest general-purpose set के लिए HashSet, जब insertion order याद रखना चाहिए तो LinkedHashSet, और जब elements को हमेशा sorted रहना चाहिए तो TreeSet।

उदाहरण: Choosing a Set Implementation

java
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);
	}
}
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. एक set से insertion order रखने की उम्मीद करना, जो HashSet नहीं करता।
  2. Index से एक element पाने की कोशिश करना, जो sets support नहीं करते।
  3. Proper equals() और hashCode() के बिना objects जोड़ना, इसलिए duplicates detect नहीं होते।

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.