Java - Set Interface and Related Classes

Set Interface

The Set interface is a Collection that stores individual data values. It does not allow duplicate data and does not guarantee order. Set does not define any new methods. Subclasses that implement the Set interface include TreeSet and HashSet.

  • Set
    • SortedSet
      • TreeSet
    • HashSet

SortedSet Interface

One interface that extends the Set interface is SortedSet, and the SortedSet interface can store data sorted in ascending order.

SortedSet Methods

Method Description
Comparator<? super E> comparator() Returns the Comparator configured for the SortedSet. If no Comparator is configured, null is returned.
E first() Returns the first object in the SortedSet.
SortedSet<E> headSet(E toElement) Returns a SortedSet object made up of the elements smaller than the toElement object.
E last() Returns the last object in the SortedSet.
SortedSet<E> subSet(E fromElement, E toElement) Returns a SortedSet object made up of elements greater than or equal to fromElement(inclusive) and smaller than toElement(exclusive).
SortedSet<E> tailSet(E fromElement) Returns a SortedSet made up of elements greater than or equal to fromElement(inclusive).

HashSet Class

HashSet extends AbstractSet and implements the Set interface. It creates a Collection that uses a hash table for storage.

A hash table stores information using a mechanism called hashing. Hash table storage stores data as a set of a uniquely distinguishable key value and a value. At this time, the key value is not assigned by the developer but is handled automatically in the code. Because stored data is distinguished and stored by hash code instead of being managed sequentially, you cannot access a key value using an index. In hashing, the information content of the key is used to determine a unique value called a hash code. When hashing is used, adding, deleting, and searching are faster when managing large amounts of data than when managing data sequentially.

Because HashSet does not support sorting, use the TreeSet class, which implements SortedSet, when you need sorting functionality.

HashSet Example

package com.devkuma.tutorial.java.util;

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

public class HashSetClass {

    public static void main(String[] args) {

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

        hs.add(85);
        hs.add(67);
        hs.add(58);
        hs.add(30);
        hs.add(46);

        for (Integer i : hs) {
            System.out.println(i);
        }
    }
}

The execution result is as follows.

67
85
58
30
46

TreeSet Class

TreeSet is a class that implements the Set interface and the SortedSet interface. It supports sorting and manages data in a tree structure. Because elements are stored in ascending order when they are added, retrieving data does not become slow.

TreeSet Example

package com.devkuma.tutorial.java.util;

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

public class TreeSetClass {

    public static void main(String[] args) {

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

        ts.add(85);
        ts.add(67);
        ts.add(58);
        ts.add(30);
        ts.add(46);

        for (Integer i : ts) {
            System.out.println(i);
        }
    }
}

The execution result is as follows.

30
46
58
67
85

Unlike the previous HashSet class, it outputs the stored data after sorting it.