Java 8 – Find Third Longest String in an Arrays or List or Stream ?

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
  • Finally, we will print 3rd longest String to the console

FindThirdLongestStringInAnArraysUsingJava8Stream.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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:

1
2
3
4
5
6
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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:

1
2
3
4
5
6
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
  • Finally, we will print 3rd longest String to the console

FindThirdLongestStringInListUsingJava8Stream.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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:

1
2
3
4
5
6
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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:

1
2
3
4
5
6
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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – Find Third Shortest String in an Arrays or List or Stream ?
Java – Find Shortest String in an Arrays or List ?