Get the first and last elements of the Set or HashSet in Java [2 ways]

In this post, I will be sharing how to get the first and last elements of the Set or HashSet in Java with examples. There are two ways to achieve our goal:

1. Using Stream API

2. Using an iterator

Read Also: How to iterate a HashSet in Java

Let's dive deep into the topic:

Get the first and last element of the Set or HashSet

1. Using Stream API


We can easily get the first element of the Set(or HashSet) using the findFirst() method of Stream API that returns Optional<T> and we can invoke the get() method on Optional<T> to obtain the final result as shown below in the example.

To get the last element of the Set(or HashSet), we can use Stream API reduce() method that returns Optional<T>. We will invoke the get() method on Optional<T> to obtain the final result as shown below in the example.

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

public class FirstLastElementSet {
    public static void main(String args[]) {
      String firstElement = null;
      String lastElement = null;
      
      // Creating object of HashSet
      Set<String> brands = new HashSet<>();
      brands.add("Apple");
      brands.add("Amazon");
      brands.add("Google");
      brands.add("Oracle");
      
      // Printing the Original HashSet elements
      System.out.println(brands);
      
      // Getting first element 
      firstElement = brands.stream().findFirst().get();
      System.out.println("First element is: "+ firstElement);
      
      // Getting last element 
      lastElement = brands.stream().reduce((first, second) -> second).get();
      System.out.println("Last element is: "+ lastElement);      
    }
}

Output:
[Google, Apple, Amazon, Oracle]
First element is: Google
Last element is: Oracle


2. Using an iterator


Using an iterator also, we can find the first and last elements of the Set or HashSet in Java as shown below in the example:

To get the first element just use set.iterator().next() whereas to get the last element just iterate the HashSet object from the beginning till the end.

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

public class FirstLastElementSet2 {
    public static void main(String args[]) {
      String firstSetElement = null;
      String lastSetElement = null;
      
      // Creating object of HashSet class
      Set<String> currencies = new HashSet<>();
      currencies.add("Rupee");
      currencies.add("Dollar");
      currencies.add("Pound");
      currencies.add("Yen");
      
      // Printing the Original HashSet elements
      System.out.println(currencies);
      
      // Getting first element
      if (!currencies.isEmpty())
      {
        firstSetElement = currencies.iterator().next();
      }    
      System.out.println("First element is: "+ firstSetElement);
      
      // Getting last element
      Iterator<String> iterator = currencies.iterator();
      while(iterator.hasNext())
      {
        lastSetElement = iterator.next();
      }    
      System.out.println("Last element is: "+ lastSetElement);      
    }
}

Output:
[Yen, Dollar, Rupee, Pound]
First element is: Yen
Last element is: Pound


That's all for today. Please mention in the comments if you have any questions related to how to get the first and last elements of the Set or HashSet in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry