data-driven-docs

Selft training repo


Project maintained by ggranados Hosted on GitHub Pages — Theme by mattgraham

Set Interface


Table of Contents


Set Interface Characteristics

In Java, the Set interface is a part of the Java Collections Framework and is used to store a collection of unique elements, meaning that duplicates are not allowed.

The Set interface is particularly useful when you need to ensure the uniqueness of elements in a collection.

Key characteristics of the Set interface:

Back to top

Most Used Set Implementations

Java provides several implementations of the Set interface. Let’s go through the most commonly used ones:

Back to top

HashSet

It’s preferred when you need a fast and unordered Set implementation and uniqueness is essential.

Example:

import java.util.Set;
import java.util.HashSet;

public class SetDemo {
    public static void main(String[] args) {
// Demonstration of HashSet
        Set<String> hashSet = new HashSet<>();

        hashSet.add("Apple");
        hashSet.add("Banana");
        hashSet.add("Orange");
        hashSet.add("Grapes");

        System.out.println("HashSet elements: " + hashSet);
        System.out.println("HashSet size: " + hashSet.size());

        hashSet.remove("Orange");
        System.out.println("HashSet after removing 'Orange': " + hashSet);

        System.out.println("Iterating through HashSet:");
        for (String fruit : hashSet) {
            System.out.println(fruit);
        }
        System.out.println();

    }
}

Hash Collision

A hash collision occurs in a hash table or hash-based data structure when two different keys produce the same hash code or index in the underlying data storage.

HashSet uses separate chaining. When two distinct objects have the same hash code, they will be placed in the same bucket in the internal hash table.

Back to top

TreeSet

It’s preferred when you need elements to be sorted in natural order or a custom comparator-defined order.

Example:

import java.util.Set;
import java.util.TreeSet;

public class SetDemo {
    public static void main(String[] args) {
// Demonstration of TreeSet
        Set<String> treeSet = new TreeSet<>();

        treeSet.add("Apple");
        treeSet.add("Banana");
        treeSet.add("Orange");
        treeSet.add("Grapes");

        System.out.println("TreeSet elements: " + treeSet);
        System.out.println("TreeSet size: " + treeSet.size());

        treeSet.remove("Orange");
        System.out.println("TreeSet after removing 'Orange': " + treeSet);

        System.out.println("Iterating through TreeSet:");
        for (String fruit : treeSet) {
            System.out.println(fruit);
        }
        System.out.println();

    }
}

Back to top

LinkedHashSet

Most preferred when you need a Set that maintains insertion order while preserving uniqueness.

Example:

import java.util.LinkedHashSet;
import java.util.Set;

public class SetDemo {
    public static void main(String[] args) {

        // Demonstration of LinkedHashSet
        Set<String> linkedHashSet = new LinkedHashSet<>();

        linkedHashSet.add("Apple");
        linkedHashSet.add("Banana");
        linkedHashSet.add("Orange");
        linkedHashSet.add("Grapes");

        System.out.println("LinkedHashSet elements: " + linkedHashSet);
        System.out.println("LinkedHashSet size: " + linkedHashSet.size());

        linkedHashSet.remove("Orange");
        System.out.println("LinkedHashSet after removing 'Orange': " + linkedHashSet);

        System.out.println("Iterating through LinkedHashSet:");
        for (String fruit : linkedHashSet) {
            System.out.println(fruit);
        }
    }
}

Back to top

Choose the appropriate implementation

Always consider the trade-offs and performance characteristics to make an informed decision.

If you need fast operations and don’t care about the order, go with HashSet.

For sorted elements, TreeSet is a good choice.

If you need to maintain insertion order, LinkedHashSet fits the bill.

Back to top

Comparison Table

Characteristic HashSet TreeSet LinkedHashSet
Underlying Data Structure Hash Table Red-Black Tree Hash Table
Ordering No ordering Sorted according to natural order Insertion order
Duplicates Allowed No No No
Null Elements Allowed Yes No Yes
Iteration Performance O(n) O(log n) O(n)
Use Cases When order doesn’t matter When elements need to be sorted When order of insertion is important

Back to top


Ref.

https://www.geeksforgeeks.org/set-in-java/?ref=lbp https://docs.oracle.com/javase/8/docs/api/java/util/Set.html


Get Started | Languages | Java Development | Original Collections API | Updated Collections