In this article, we will discuss how to get First and Last elements from an Arrays using Java 8 Streams API
Find First and Last elements in an Arrays
- Using Java 8 Streams API
- Before Java 8 release
1. Using Java 8 Streams API
- To find first element in an Arrays, 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 ArrayList, 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
FindFirstAndLastElementInArrayListInJava8.java
package in.bench.resources.find.array;
import java.util.Arrays;
public class FindFirstAndLastElementInArrayListInJava8 {
public static void main(String[] args) {
// local variables
String first = null;
String last = null;
// create String[] arrays
String[] names = {
"Deepinder Goyal",
"Vinay Sanghi",
"Bipin Preet Singh",
"Vijay Shekhar Sharma",
"Falguni Nayar"
};
// find First element in Arrays
first = Arrays.stream(names).findFirst().get();
// find Last element in Arrays
last = Arrays.stream(names).reduce((one, two) -> two).get();
// print to console
System.out.println("First name in the Arrays is = " + first);
System.out.println("Last name in the Arrays is = " + last);
}
}
Output:
First name in the Arrays is = Deepinder Goyal
Last name in the Arrays is = Falguni Nayar
2. Before Java 8 Release
- To find first and last elements in an Arrays, check whether Arrays length is greater-than zero
- If Arrays length is greater-than zero, then
- get first element using [index] position by passing 0th index
- get last element using [index] position by passing last index of the Arrays i.e., [arr.length -1]
FindFirstAndLastElementInArrays.java
package in.bench.resources.find.array;
public class FindFirstAndLastElementInArrays {
public static void main(String[] args) {
// local variables
String first = null;
String last = null;
// create String[] arrays
String[] names = {
"Deepinder Goyal",
"Vinay Sanghi",
"Bipin Preet Singh",
"Vijay Shekhar Sharma",
"Falguni Nayar"
};
// find first and last element in Arrays
if(null != names && names.length > 0) {
first = names[0];
last = names[names.length - 1];
}
// print to console
System.out.println("First name in the Arrays is = " + first);
System.out.println("Last name in the Arrays is = " + last);
}
}
Output:
First name in the Arrays is = Deepinder Goyal
Last name in the Arrays is = Falguni Nayar
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/Arrays.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 !!