Ugenz Academy/Tutorial

Set

4 min read697 words

Reading progress

Keep learning

Set in Java

Introduction

A Set in Java is a collection that is used to store unique elements. Unlike a List, a Set does not allow duplicate values.

The Set interface is part of the Java Collection Framework and is available in the java.util package.

import java.util.Set;

Features of Set

  • Does not allow duplicate elements.
  • Stores unique values.
  • Some Set implementations maintain insertion order.
  • Supports dynamic size.
  • Provides methods to add, remove, and search elements.
  • Can store null depending on the implementation.

Set Syntax

Set<Type> setName = new HashSet<>();

Example:

Set<String> names = new HashSet<>();

Example of Set

import java.util.*; public class SetExample { public static void main(String[] args) { Set<String> names = new HashSet<>(); names.add("Anu"); names.add("Ram"); names.add("Sita"); names.add("Anu"); System.out.println(names); } }

Output

[Anu, Sita, Ram]

The duplicate "Anu" is not stored.

Note: HashSet does not guarantee insertion order, so the output order may be different.

Common Set Methods

MethodDescription
add()Adds an element
remove()Removes an element
contains()Checks whether an element exists
size()Returns the number of elements
isEmpty()Checks whether the Set is empty
clear()Removes all elements
addAll()Adds elements from another collection
containsAll()Checks whether all specified elements exist

Example of Set Methods

import java.util.*; public class SetMethods { public static void main(String[] args) { Set<Integer> numbers = new HashSet<>(); numbers.add(10); numbers.add(20); numbers.add(30); System.out.println(numbers.contains(20)); numbers.remove(10); System.out.println(numbers); System.out.println(numbers.size()); } }

Output

true [20, 30] 2

Types of Set

The commonly used Set implementations are:

1. HashSet

HashSet stores unique elements and does not guarantee any particular order.

Set<String> names = new HashSet<>(); names.add("Anu"); names.add("Ram"); names.add("Sita"); System.out.println(names);

2. LinkedHashSet

LinkedHashSet maintains the insertion order of elements.

Set<String> names = new LinkedHashSet<>(); names.add("Anu"); names.add("Ram"); names.add("Sita"); System.out.println(names);

Output

[Anu, Ram, Sita]

3. TreeSet

TreeSet stores unique elements in sorted order.

Set<Integer> numbers = new TreeSet<>(); numbers.add(30); numbers.add(10); numbers.add(20); System.out.println(numbers);

Output

[10, 20, 30]

Set vs List

SetList
Does not allow duplicatesAllows duplicates
Mainly used for unique elementsUsed for ordered elements
Generally does not use indexesSupports index-based access
HashSet, LinkedHashSet, TreeSetArrayList, LinkedList, Vector
TreeSet stores sorted elementsMaintains insertion order

Set vs Array

SetArray
Dynamic sizeFixed size
Does not allow duplicatesAllows duplicates
Provides collection methodsLimited built-in methods
Part of Collection FrameworkBasic Java structure
Useful for unique dataUseful for fixed-size data

Real-Life Examples

A Set is useful when duplicate data should not be stored.

Examples:

  • Unique student IDs
  • Unique email addresses
  • Unique usernames
  • Unique phone numbers
  • Unique product codes
  • Unique city names

For example:

Student IDs ---------101 102 103 101

After storing them in a Set:

101 102 103

The duplicate 101 is automatically removed.

Advantages of Set

  1. Prevents duplicate values.
  2. Useful for storing unique data.
  3. Provides useful methods for searching and removing elements.
  4. Supports dynamic size.
  5. TreeSet can automatically sort elements.
  6. LinkedHashSet can preserve insertion order.

Key Points

  • Set is an interface in Java.
  • It belongs to the java.util package.
  • A Set does not allow duplicate elements.
  • HashSet does not guarantee order.
  • LinkedHashSet maintains insertion order.
  • TreeSet stores elements in sorted order.
  • Set is useful when uniqueness is important.

Conclusion

A Set is an important collection in Java for storing unique elements. It is especially useful when duplicate values must be avoided. The most commonly used implementations are HashSet, LinkedHashSet, and TreeSet.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy