Explain HashSet, LinkedHashSet and TreeSet

Explain HashSet, LinkedHashSet and TreeSet

Explanation

1. HashSet

  • Structure: HashSet is a collection that does not allow duplicate elements. It is backed by a hash table (actually a HashMap instance).
  • Ordering: HashSet does not guarantee any specific iteration order of the elements.
  • Performance: Provides constant time performance for basic operations like add, remove, contains, and size, assuming the hash function disperses elements properly among the buckets.

2. LinkedHashSet

  • Structure: LinkedHashSet extends HashSet but maintains a doubly-linked list running through all of its entries.
  • Ordering: This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order).
  • Performance: Offers performance similar to HashSet but with a slightly overhead for maintaining the linked list.

3. TreeSet

  • Structure: TreeSet is a NavigableSet implementation based on a TreeMap.
  • Ordering: It stores elements in a sorted and ascending order according to their natural ordering or by a specified comparator.
  • Erformance: Provides guaranteed log(n) time cost for the basic operations (add, remove, and contains).

HashSet, LinkedHashSet and TreeSet

java
Here's a Java example demonstrating the differences between these three sets:

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;

public class SetExample {
    public static void main(String[] args) {
        // HashSet Example
        HashSet<String> hashSet = new HashSet<>();
        hashSet.add("Banana");
        hashSet.add("Apple");
        hashSet.add("Cherry");
        hashSet.add("Date");

        System.out.println("HashSet: " + hashSet);
        
        // LinkedHashSet Example
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
        linkedHashSet.add("Banana");
        linkedHashSet.add("Apple");
        linkedHashSet.add("Cherry");
        linkedHashSet.add("Date");

        System.out.println("LinkedHashSet: " + linkedHashSet);
        
        // TreeSet Example
        TreeSet<String> treeSet = new TreeSet<>();
        treeSet.add("Banana");
        treeSet.add("Apple");
        treeSet.add("Cherry");
        treeSet.add("Date");

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

Explanation HashSet , LinkedHashSet and TreeSet

  • The HashSet does not guarantee the order of elements. When you print the HashSet, the elements may not appear in the order you inserted them.
  • Example output: HashSet: [Banana, Cherry, Apple, Date]
  • LinkedHashSet:
  • The LinkedHashSet maintains the insertion order. When you print the LinkedHashSet, the elements will appear in the order you inserted them.

Example output

  • LinkedHashSet: [Banana, Apple, Cherry, Date]
  • TreeSet:
  • The TreeSet maintains the elements in a sorted order. When you print the TreeSet, the elements will appear sorted in ascending order.
  • Example output: TreeSet: [Apple, Banana, Cherry, Date]

Homepage

Readmore