0% found this document useful (0 votes)
1 views10 pages

Java Set Interface Complete Notes

The Set interface in Java is part of the Collections Framework and represents a collection of unique elements, disallowing duplicates and providing various implementations like HashSet, LinkedHashSet, and TreeSet. Each implementation has distinct properties, such as ordering and performance characteristics, making them suitable for different use cases. Key methods of the Set interface include add(), remove(), contains(), and size(), and it is primarily used for scenarios requiring unique data, such as attendance systems and email registrations.

Uploaded by

iniyansumathi85
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views10 pages

Java Set Interface Complete Notes

The Set interface in Java is part of the Collections Framework and represents a collection of unique elements, disallowing duplicates and providing various implementations like HashSet, LinkedHashSet, and TreeSet. Each implementation has distinct properties, such as ordering and performance characteristics, making them suitable for different use cases. Key methods of the Set interface include add(), remove(), contains(), and size(), and it is primarily used for scenarios requiring unique data, such as attendance systems and email registrations.

Uploaded by

iniyansumathi85
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🔹 SET INTERFACE IN JAVA (COMPLETE NOTES)

1️⃣ What is Set Interface?

📘 Definition

The Set interface is part of the Java Collections Framework that represents a collection of unique
elements.
👉 It does not allow duplicate values.

📌 Package:

[Link]

📌 Key Property: - No duplicates - At most one null (depends on implementation) - No index-based


access

🔹 Real-Time Example

📌 Attendance System
A student can mark attendance only once per day.

Student IDs: 101, 102, 101, 103


Stored in Set → 101, 102, 103

Duplicates are automatically removed.

2️⃣ Why Set is Needed?

Problem Solution using Set

Duplicate entries Set removes duplicates

Unique IDs Set ensures uniqueness

Fast searching HashSet gives O(1) average

1
3️⃣ Hierarchy of Set Interface

Set (Interface)
├── HashSet
├── LinkedHashSet
└── TreeSet

4️⃣ HASHSET

🔹 Definition

HashSet stores elements using hashing and does not maintain insertion order.

📌 Underlying DS: Hash Table

🔹 Properties

• ❌ No duplicates
• ❌ No order guarantee
• ✔ Allows one null
• ✔ Fast performance

🔹 Syntax

HashSet<Type> set = new HashSet<>();

🔹 Real-Time Example

📌 Email Registration System


Each email must be unique.

🔹 Code Example

import [Link];

public class HashSetExample {


public static void main(String[] args) {
HashSet<String> emails = new HashSet<>();

2
[Link]("a@[Link]");
[Link]("b@[Link]");
[Link]("a@[Link]"); // duplicate

[Link](emails);
}
}

🔹 Output (order may vary)

[a@[Link], b@[Link]]

5️⃣ LINKEDHASHSET

🔹 Definition

LinkedHashSet is a subclass of HashSet that maintains insertion order.

📌 Underlying DS: - Hash Table + Doubly Linked List

🔹 Properties

• ❌ No duplicates
• ✔ Maintains insertion order
• ✔ Allows one null
• ✔ Slightly slower than HashSet

🔹 Syntax

LinkedHashSet<Type> set = new LinkedHashSet<>();

🔹 Real-Time Example

📌 Order of Login Attempts


Maintain the order of unique login users.

3
🔹 Code Example

import [Link];

public class LinkedHashSetExample {


public static void main(String[] args) {
LinkedHashSet<Integer> set = new LinkedHashSet<>();

[Link](10);
[Link](20);
[Link](10);
[Link](30);

[Link](set);
}
}

🔹 Output

[10, 20, 30]

6️⃣ TREESET

🔹 Definition

TreeSet stores elements in sorted order.

📌 Underlying DS: Red-Black Tree

🔹 Properties

• ❌ No duplicates
• ✔ Sorted order (Ascending by default)
• ❌ No null values
• ✔ Slower than HashSet

🔹 Syntax

TreeSet<Type> set = new TreeSet<>();

4
🔹 Real-Time Example

📌 Leaderboard Scores
Scores must be unique and sorted.

🔹 Code Example

import [Link];

public class TreeSetExample {


public static void main(String[] args) {
TreeSet<Integer> scores = new TreeSet<>();

[Link](50);
[Link](10);
[Link](30);

[Link](scores);
}
}

🔹 Output

[10, 30, 50]

7️⃣ SET METHODS (IMPORTANT)

Method Description

add() Adds element

remove() Removes element

contains() Checks presence

size() Total elements

isEmpty() Empty or not

clear() Removes all

iterator() Traversing

5
🔹 Example

Set<Integer> set = new HashSet<>();


[Link](10);
[Link](20);

[Link]([Link](10)); // true
[Link]([Link]()); // 2

8️⃣ ORDERING & UNIQUENESS

🔹 Uniqueness

✔ All Set implementations do not allow duplicates

[Link](10);
[Link](10); // ignored

🔹 Ordering Comparison

Set Type Order

HashSet ❌ Random

LinkedHashSet ✔ Insertion order

TreeSet ✔ Sorted order

9️⃣ SET vs LIST

Feature Set List

Duplicates ❌ No ✔ Yes

Order Depends ✔ Maintained

Index access ❌ No ✔ Yes

Null values One or none Multiple

Use case Unique data Ordered data

6
🔹 Real-Time Example

📌 Phone Contacts → Set


📌 Playlist Songs → List

🔟 PRACTICE PROBLEMS (WITH CODE)

🧠 Problem 1: Remove Duplicates

Input:

10 20 10 30 20

Output:

10 20 30

import [Link].*;

public class RemoveDuplicates {


public static void main(String[] args) {
int[] arr = {10,20,10,30,20};

Set<Integer> set = new HashSet<>();


for(int x : arr) {
[Link](x);
}

[Link](set);
}
}

🧠 Problem 2: Maintain Insertion Order

Input:

A B A C

Output:

7
A B C

import [Link].*;

public class LinkedHashSetDemo {


public static void main(String[] args) {
Set<String> set = new LinkedHashSet<>();

[Link]("A");
[Link]("B");
[Link]("A");
[Link]("C");

[Link](set);
}
}

🧠 Problem 3: Sort Unique Numbers

Input:

5 3 1 5 2

Output:

1 2 3 5

import [Link].*;

public class TreeSetSort {


public static void main(String[] args) {
int[] arr = {5,3,1,5,2};

Set<Integer> set = new TreeSet<>();


for(int x : arr) {
[Link](x);
}

[Link](set);
}
}

8
🧠 Problem 4: Count Unique Elements

Input:

1 2 2 3 4 4

Output:

import [Link].*;

public class CountUnique {


public static void main(String[] args) {
int[] arr = {1,2,2,3,4,4};

Set<Integer> set = new HashSet<>();


for(int x : arr) {
[Link](x);
}

[Link]([Link]());
}
}

🧠 Problem 5: Check Element Exists

Input:

Set: 10 20 30
Check: 20

Output:

Element Found

import [Link].*;

public class SearchInSet {


public static void main(String[] args) {
Set<Integer> set = new HashSet<>();

9
[Link](10);
[Link](20);
[Link](30);

if([Link](20)) {
[Link]("Element Found");
} else {
[Link]("Not Found");
}
}
}

✅ INTERVIEW SUMMARY (IMPORTANT)


• Use Set for unique data
• HashSet → Fastest
• LinkedHashSet → Order matters
• TreeSet → Sorted data
• No index, no duplicates

10

You might also like