In this article, we will discuss how to find third longest String in an Arrays and List using Java 8 Stream
1. Finding Third Longest String in an Arrays:
We will follow below 2 approaches to get 3rd Longest String in an Arrays
- Using Stream.skip() method
- Using Stream.limit() & Stream.skip() methods
1.1 Using Stream.skip() method
- First, get Stream from Arrays using Arrays.stream() method
- Sort String[] Arrays in descending-order using Comparator.comparing(String::length).reversed() inside Stream.sorted() method
- As Arrays sorted in descending-order, 3rd element in the Arrays will be the third longest String
- We will skip first 2 Strings which is the longest and 2nd longest Strings using Stream.skip() method
- Stream.findFirst() method will return 3rd longest String in the Arrays
- Finally, we will print 3rd longest String to the console
FindThirdLongestStringInAnArraysUsingJava8Stream.java
package in.bench.resources.third.longest.string;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;
public class FindThirdLongestStringInAnArraysUsingJava8Stream {
public static void main(String[] args) {
// 1. names with different length
String[] names = new String[] {
"Bond",
"Einstein",
"Alice",
"Whitman",
"Bob",
"Spider"
};
// 1.1 print to console
System.out.println("Original String[] Arrays :- \n"
+ Arrays.toString(names));
// 2. Execution - start time
LocalTime startTime = LocalTime.now();
// 2.1 sort in descending-order acc. to String length
String thirdLongestString = Arrays
.stream(names)
.sorted(Comparator.comparing(String::length).reversed())
.skip(2)
.findFirst()
.get();
// 2.2 Execution - end time
LocalTime endTime = LocalTime.now();
// 2.3 find difference
Duration duration = Duration.between(startTime, endTime);
long differenceInNano = duration.getNano();
// 2.4 print sum to console
System.out.println("\nThird longest String in an Arrays is - "
+ thirdLongestString);
// 2.5 print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Original String[] Arrays :-
[Bond, Einstein, Alice, Whitman, Bob, Spider]
Third longest String in an Arrays is - Spider
Execution time - 15395600 ns
1.2 Using Stream.limit() & Stream.skip() methods
- This example is very similar to above example 1.1 except that we are limiting top 3 Strings length-wise after descending-order sorting using Stream.limit() method which are the longest & 2nd longest and 3rd longest Strings in the Arrays
- Using Stream.skip() method, we are skipping first 2 elements and remaining one element in the Arrays is the 3rd longest String
FindThirdLongestStringInAnArraysUsingJava8Stream.java
package in.bench.resources.third.longest.string;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;
public class FindThirdLongestStringInAnArraysUsingJava8Stream {
public static void main(String[] args) {
// 1. names with different length
String[] names = new String[] {
"Bond",
"Einstein",
"Alice",
"Whitman",
"Bob",
"Spider"
};
// 1.1 print to console
System.out.println("Original String[] Arrays :- \n"
+ Arrays.toString(names));
// 2. Execution - start time
LocalTime startTime = LocalTime.now();
// 2.1 sort in descending-order acc. to String length
String thirdLongestString = Arrays
.stream(names)
.sorted(Comparator.comparing(String::length).reversed())
.limit(3)
.skip(2)
.findFirst()
.get();
// 2.2 Execution - end time
LocalTime endTime = LocalTime.now();
// 2.3 find difference
Duration duration = Duration.between(startTime, endTime);
long differenceInNano = duration.getNano();
// 2.4 print sum to console
System.out.println("\nThird longest String in an Arrays is - "
+ thirdLongestString);
// 2.5 print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Original String[] Arrays :-
[Bond, Einstein, Alice, Whitman, Bob, Spider]
Third longest String in an Arrays is - Spider
Execution time - 16998200 ns
2. Finding Third Longest String in List or ArrayList:
We will follow below 2 approaches to get 3rd Longest String in a List or ArrayList
- Using Stream.skip() method
- Using Stream.limit() & Stream.skip() methods
2.1 Using Stream.skip() method
- First, get Stream from List using List.stream() method
- Sort String List in descending-order using Comparator.comparing(String::length).reversed() inside Stream.sorted() method
- As List is sorted in descending-order, 3rd element in the List will be the third longest String
- We will skip first 2 Strings which is the longest and 2nd longest Strings using Stream.skip() method
- Stream.findFirst() method will return 3rd longest String in the List
- Finally, we will print 3rd longest String to the console
FindThirdLongestStringInListUsingJava8Stream.java
package in.bench.resources.third.longest.string;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class FindThirdLongestStringInListUsingJava8Stream {
public static void main(String[] args) {
// 1. names with different length
List<String> names = Arrays.asList(
"Bond",
"Einstein",
"Alice",
"Whitman",
"Bob",
"Spider"
);
// 1.1 print to console
System.out.println("Original String List :- \n"
+ names);
// 2. Execution - start time
LocalTime startTime = LocalTime.now();
// 2.1 sort in descending-order acc. to String length
String thirdLongestString = names
.stream()
.sorted(Comparator.comparing(String::length).reversed())
.skip(2)
.findFirst()
.get();
// 2.2 Execution - end time
LocalTime endTime = LocalTime.now();
// 2.3 find difference
Duration duration = Duration.between(startTime, endTime);
long differenceInNano = duration.getNano();
// 2.4 print sum to console
System.out.println("\nThird longest String in List is - "
+ thirdLongestString);
// 2.5 print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Original String List :-
[Bond, Einstein, Alice, Whitman, Bob, Spider]
Third longest String in List is - Spider
Execution time - 15389500 ns
2.2 Using Stream.limit() & Stream.skip() methods
- This example is very similar to above example 2.1 except that we are limiting top 3 Strings length-wise after descending-order sorting using Stream.limit() method which are the longest & 2nd longest and 3rd longest Strings in the List
- Using Stream.skip() method, we are skipping first 2 elements and remaining one element in the List is the 3rd longest String
FindThirdLongestStringInListUsingJava8Stream.java
package in.bench.resources.third.longest.string;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class FindThirdLongestStringInListUsingJava8Stream {
public static void main(String[] args) {
// 1. names with different length
List<String> names = Arrays.asList(
"Bond",
"Einstein",
"Alice",
"Whitman",
"Bob",
"Spider"
);
// 1.1 print to console
System.out.println("Original String List :- \n"
+ names);
// 2. Execution - start time
LocalTime startTime = LocalTime.now();
// 2.1 sort in descending-order acc. to String length
String thirdLongestString = names
.stream()
.sorted(Comparator.comparing(String::length).reversed())
.limit(3)
.skip(2)
.findFirst()
.get();
// 2.2 Execution - end time
LocalTime endTime = LocalTime.now();
// 2.3 find difference
Duration duration = Duration.between(startTime, endTime);
long differenceInNano = duration.getNano();
// 2.4 print sum to console
System.out.println("\nThird longest String in List is - "
+ thirdLongestString);
// 2.5 print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Original String List :-
[Bond, Einstein, Alice, Whitman, Bob, Spider]
Third longest String in List is - Spider
Execution time - 15393400 ns
3. Points to remember w.r.t execution time:
- Execution time differs in different platforms
- With small set of numbers, we may not find large difference in execution time
- But with large set of numbers, difference will be significant to consider
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/stream/Stream.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#skip-long-
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#limit-long-
- https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#stream-int:A-
Happy Coding !!
Happy Learning !!