Java 8 – Find First and Last elements in a Set or HashSet

In this article, we will discuss how to get First and Last elements from a Set or HashSet using Java 8 Streams API

Find First and Last elements in HashSet

  1. Using Java 8 Streams API
  2. Before Java 8 release

Note: Set or HashSet stores elements in random-order and doesn’t allows duplicates

1. Using Java 8 Streams API

FindFirstAndLastElementInHashSetInJava8.java

package in.bench.resources.find.set;

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

public class FindFirstAndLastElementInHashSetInJava8 {

	public static void main(String[] args) {

		// local variables
		String first = null;
		String last = null;


		// create HashSet object
		Set<String> names = new HashSet<>();


		// add names to newly created HashSet
		names.add("Deepinder Goyal");
		names.add("Vinay Sanghi");
		names.add("Bipin Preet Singh");
		names.add("Vijay Shekhar Sharma");
		names.add("Falguni Nayar");


		System.out.println("Orignal HashSet elements :- \n");
		names.forEach(System.out::println);


		// find First element in HashSet
		first = names.stream().findFirst().get();


		// find Last element in HashSet
		last = names.stream().reduce((one, two) -> two).get();


		// print to console
		System.out.println("\n\nFirst name in the Set is = " + first);
		System.out.println("Last name in the Set is = " + last);
	}
}

Output:

Orignal HashSet elements :- 

Deepinder Goyal
Bipin Preet Singh
Vijay Shekhar Sharma
Falguni Nayar
Vinay Sanghi


First name in the Set is = Deepinder Goyal
Last name in the Set is = Vinay Sanghi

2. Before Java 8 Release

  • To find first and last elements in a HashSet, use Iterator
  • For First element,
    • use names.iterator().next(); method
  • For Last element,
    • Iterate HashSet from beginning till the end and set retrieved element to “last” variable every time
    • This way during last iteration of HashSet, last element will be set to “last” variable

FindFirstAndLastElementInHashSet.java

package in.bench.resources.find.set;

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

public class FindFirstAndLastElementInHashSet {

	public static void main(String[] args) {

		// local variables
		String first = null;
		String last = null;


		// create HashSet object
		Set<String> names = new HashSet<>();


		// add names to newly created HashSet
		names.add("Deepinder Goyal");
		names.add("Vinay Sanghi");
		names.add("Bipin Preet Singh");
		names.add("Vijay Shekhar Sharma");
		names.add("Falguni Nayar");


		System.out.println("Orignal HashSet elements :- \n");
		for(String name : names) {
			System.out.println(name);
		}


		// find First element in HashSet
		if(!names.isEmpty()) {
			first = names.iterator().next();
		}


		// find Last element in HashSet
		Iterator<String> iterator = names.iterator();
		while(iterator.hasNext()){
			last = iterator.next();
		}  


		// print to console
		System.out.println("\n\nFirst name in the Set is = " + first);
		System.out.println("Last name in the Set is = " + last);
	}
}

Output:

Orignal HashSet elements :- 

Deepinder Goyal
Bipin Preet Singh
Vijay Shekhar Sharma
Falguni Nayar
Vinay Sanghi


First name in the Set is = Deepinder Goyal
Last name in the Set is = Vinay Sanghi

References:

Happy Coding !!
Happy Learning !!

Java 8 – Find First and Last entries in a Map or HashMap
Java 8 – Find First and Last elements in an Arrays