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
- Using Java 8 Streams API
- Before Java 8 release
Note: Set or HashSet stores elements in random-order and doesn’t allows duplicates
1. Using Java 8 Streams API
- To find first element in a HashSet, we can use findFirst() method of Stream API which returns Optional<T> and
- We can invoke get() method on Optional<T> to obtain the final result
- Similarly, to get last element from HashSet, we can use reduce() method of Stream API which returns Optional<T> and
- We can invoke get() method on Optional<T> to obtain the final result
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
Related Articles:
- Java 8 – Find Largest number in an Arrays or List or Stream
- Java 8 – Find Smallest number in an Arrays or List or Stream
- Java 8 – Find 2nd Largest number in an Arrays or List or Stream
- Java 8 – Find 2nd Smallest number in an Arrays or List or Stream
- Java 8 – Find sum of Largest 2 numbers in an Arrays or List or Stream
- Java 8 – Find sum of Smallest 2 numbers in an Arrays or List or Stream
- Java 8 – Find 1st and Last elements in an Arrays
- Java 8 – Find 1st and Last elements in a List or ArrayList
- Java 8 – Find 1st and Last elements in a Set or HashSet
- Java 8 – Find 1st and Last entries in a Map or HashMap
- Java 8 – Find sum and average of a List or ArrayList
- Java 8 – How to calculate sum and average of an Arrays ?
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/Set.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#findFirst–
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#reduce-java.util.function.BinaryOperator-
- https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#get–
Happy Coding !!
Happy Learning !!