SortedSet headSet() method in Java

The headSet() method of SortedSet interface in Java is utilized to access a view of the part of the sorted set whose elements are less than a specified element only. It returns a portion of the original set keeping only elements which are less than the specified element, this method does not contain the specified element itself. It should be mentioned that SortedSet is an interface that extends Set interface and provides a definition that says the elements must be put in a particular order.

Method Signature

SortedSet<E> headSet(E toElement);  

SortedSet: The return type of this method is SortedSet, which shows that the method will return a view of the sorted set where E represents the type of the elements stored in the sorted set. SortedSet is the interface from Java's Collections Framework which extends the Set interface and therefore ensures that the elements are ordered in a specified way.

headSet: It is the name of the method. In Java, method names are used to invoke the functionality provided by the method.

E toElement: The method takes a single parameter of type E, which represents the type of elements stored in the set, this parameter is named toElement. It serves as the upper bound (exclusive) for the elements included in the returned subset. The method will return a view of the portion of the sorted set whose elements are strictly less than this specified element.

Various examples of using SortedSet headSet method

1. Using TreeSet

TreeSet is a class in Java that implements the SortedSet interface. It is backed by a Red-Black Tree, which provides guaranteed O(log n) time cost for the basic operations like add, remove, and contains.

The headSet() method in the SortedSet interface is particularly useful when you want to retrieve a subset of elements from the sorted set that are strictly less than a specified element.

Algorithm

Step 1: Import the necessary Java libraries (java.util.*) for using TreeSet and related classes.

Step 2: Create a TreeSet: Instantiate a TreeSet to store integers. This will create an empty sorted set.

Step 3: Add Elements to TreeSet: Add integer elements (10, 20, 30, 40, 50) to the TreeSet.

Step 4: Use the headSet() method of the TreeSet to create a subset containing elements strictly less than 30. Store the result in a variable named headSet.

Step 5: Manipulate the Subset: Remove 20 from the headSet subset using the remove() method.

Step 6: Print the elements of the headSet and print the elements of the original TreeSet to show that changes to the subset affect the original set.

Filename: TreeSet.java

import java.util.*;

public class TreeSet {

    public static void main(String[] args) {

        // Create a TreeSet

        SortedSet<Integer> treeSet = new TreeSet<>();

        // Add Elements to TreeSet

        treeSet.add(10);

        treeSet.add(20);

        treeSet.add(30);

        treeSet.add(40);

        treeSet.add(50);

        // Create a Head Set

        SortedSet<Integer> headSet = treeSet.headSet(30);

        // Manipulate the Subset

        headSet.remove(20); // Removes 20 from the subset

        // Print the Head Set

        System.out.println("Head Set: " + headSet);

        // Print the Original Set

        System.out.println("Original Set: " + treeSet);

    }

}

Output:

Head Set: [10]

Original Set: [10, 30, 40, 50]

2. Using String Elements

Java's SortedSet interface, which extends the Set interface, is designed to store elements in a sorted order. When dealing with string elements, the sorting is typically lexicographic, meaning it follows the natural ordering of strings.

Algorithm

Step 1: Import the necessary Java libraries and create a TreeSet with Strings: Instantiate a TreeSet to store strings, this will create an empty sorted set.

Step 2: Add string elements ("apple", "banana", "grape", "orange", "pineapple") to the TreeSet.

Step 3: Use the headSet() method of the TreeSet to create a subset containing elements strictly less than "orange". Store the result in a variable named headSet.

Step 4: Manipulate the Subset: Manipulate the obtained subset like removing an element from it. Print the elements of the headSet.

Filename: StringElements.java

import java.util.*;

public class StringElements {

    public static void main(String[] args) {

        // Create a TreeSet with Strings

        SortedSet<String> treeSet = new TreeSet<>();

        // Add String Elements to TreeSet

        treeSet.add("apple");

        treeSet.add("banana");

        treeSet.add("grape");

        treeSet.add("orange");

        treeSet.add("pineapple");

        // Create a Head Set

        SortedSet<String> headSet = treeSet.headSet("orange");

        // Manipulate the Subset (Optional)

        // For example, remove an element from the subset

        // headSet.remove("banana");

        // Print the Head Set

        System.out.println("Head Set: " + headSet);

    }

}

Output:

Head Set: [apple, banana, grape]

3. Using Custom Objects and Comparator

In Java, we can define your own classes to represent custom objects. When working with custom objects in a SortedSet, you can provide a comparator to define the ordering of these objects, this allows you to sort instances of your custom class according to specific criteria.

Algorithm

Step 1: Import the necessary Java libraries and define custom classes (Person and AgeComparator) to represent the objects and the comparator for custom sorting.

Step 2: Create SortedSet with Custom Comparator: Instantiate a TreeSet (or another implementation of SortedSet) and pass an instance of the custom comparator (AgeComparator) to the constructor.

Step 3: Add Custom Objects to the Set: Add instances of the Person class to the SortedSet.

Step 4: Use the headSet() method of the SortedSet to create a subset containing elements strictly less than a specified element (30 in this case), based on the custom sorting logic defined by the comparator.

Step 5: Manipulate the obtained subset, such as removing elements and print the elements of the subset as per the conditions.

Filename: CustomObjects.java

import java.util.*;

// Define Custom Classes

class Person {

    private String name;

    private int age;

    public Person(String name, int age) {

        this.name = name;

        this.age = age;

    }

    // Getter method for age

    public int getAge() {

        return age;

    }

    // Other methods and constructors (if needed)

    @Override

    public String toString() {

        return name + " (" + age + ")";

    }

}

// Define Comparator

class AgeComparator implements Comparator<Person> {

    @Override

    public int compare(Person p1, Person p2) {

        return Integer.compare(p1.getAge(), p2.getAge());

    }

}

public class CustomObjects {

    public static void main(String[] args) {

        // Create SortedSet with Custom Comparator

        SortedSet<Person> peopleSet = new TreeSet<>(new AgeComparator());

        // Add Custom Objects to the Set

        peopleSet.add(new Person("Alice", 25));

        peopleSet.add(new Person("Bob", 30));

        peopleSet.add(new Person("Charlie", 20));

        peopleSet.add(new Person("David", 35));

        // Create a Subset

        SortedSet<Person> youngerThan30Set = peopleSet.headSet(new Person("", 30));

        // Manipulate the Subset (Optional)

        // For example, remove an element from the subset

        // youngerThan30Set.remove(new Person("Alice", 25));

        // Print the Subset

        System.out.println("People Younger than 30: " + youngerThan30Set);

    }

}

Output:

People Younger than 30: [Charlie (20), Alice (25)]   

4. Using Reverse Ordering

In Java, the SortedSet interface provides a way to store a collection of elements sorted according to their natural ordering or using a custom comparator. By default, elements are sorted in ascending order. However, there are scenarios where you might need elements to be sorted in descending order.

Algorithm

Step 1: Import the necessary Java libraries and create a TreeSet with Reverse Ordering: Instantiate a TreeSet with reverse ordering by passing Collections.reverseOrder() to the constructor. This will create an empty sorted set sorted in descending order.

Step 2: Add Elements to TreeSet: Add integer elements (10, 20, 30, 40, 50) to the TreeSet.

Step 3: Create a Subset with Reverse Ordering: Use the headSet() method of the TreeSet to create a subset containing elements strictly less than 30.

Step 4: As the TreeSet is created with reverse ordering, the subset will contain elements strictly greater than 30 in reverse order.

Step 5: Manipulate the obtained subset like removing elements or performing other operations and print the elements of the subset as per the conditions.

Filename: ReverseOrdering.java

import java.util.*;

public class ReverseOrdering {

    public static void main(String[] args) {

        // Create a TreeSet with Reverse Ordering

        SortedSet<Integer> treeSet = new TreeSet<>(Collections.reverseOrder());

        // Add Elements to TreeSet

        treeSet.add(10);

        treeSet.add(20);

        treeSet.add(30);

        treeSet.add(40);

        treeSet.add(50);

        // Create a Subset with Reverse Ordering

        SortedSet<Integer> greaterThan30Set = treeSet.headSet(30);

        // Manipulate the Subset (Optional)

        // For example, remove an element from the subset

        // greaterThan30Set.remove(20);

        // Print the Subset

        System.out.println("Elements Greater than 30 (in reverse order): " + greaterThan30Set);

    }

}

Output:

Elements Greater than 30 (in reverse order): [50, 40] 

Conclusion

In conclusion, the headSet() method in Java's SortedSet interface provides a powerful mechanism for obtaining a subset of elements that are strictly less than a specified element. Overall, the headSet() method is a valuable tool for working with sorted collections in Java. Whether you need to obtain a subset of elements based on specific criteria or manipulate subsets efficiently, headSet() provides the functionality and flexibility needed to accomplish these tasks effectively. It support for custom sorting logic and efficient performance make it an essential component in Java's collections framework.