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

In this article, we will discuss how to find third shortest String in an Arrays and List using Java 8 Stream

1. Finding Third Shortest String in an Arrays:

We will follow below 2 approaches to get 3rd Shortest 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 ascending-order using Comparator.comparing(String::length) inside Stream.sorted() method
    • As Arrays sorted in ascending-order, 3rd element in the Arrays will be the third shortest String
  • We will skip first 2 Strings which is the shortest and 2nd shortest Strings using Stream.skip() method
  • Finally, we will print 3rd shortest String to the console

FindThirdShortestStringInAnArraysUsingJava8Stream.java

package in.bench.resources.third.shortest.string;

import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;

public class FindThirdShortestStringInAnArraysUsingJava8Stream {

	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 ascending-order acc. to String length
		String thirdShortestString = Arrays
				.stream(names)
				.sorted(Comparator.comparing(String::length))
				.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 Shortest String in an Arrays is - "
				+ thirdShortestString);


		// 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 Shortest String in an Arrays is - Alice

Execution time - 15534400 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 ascending-order sorting using Stream.limit() method which are the shortest & 2nd shortest and 3rd shortest Strings in the Arrays
  • Using Stream.skip() method, we are skipping first 2 elements and remaining one element in the Arrays is the 3rd shortest String

FindThirdShortestStringInAnArraysUsingJava8Stream.java

package in.bench.resources.third.shortest.string;

import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;

public class FindThirdShortestStringInAnArraysUsingJava8Stream {

	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 ascending-order acc. to String length
		String thirdShortestString = Arrays
				.stream(names)
				.sorted(Comparator.comparing(String::length))
				.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 Shortest String in an Arrays is - "
				+ thirdShortestString);


		// 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 Shortest String in an Arrays is - Alice

Execution time - 16018600 ns

2. Finding Third Shortest String in List or ArrayList:

We will follow below 2 approaches to get 3rd Shortest 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 ascending-order using Comparator.comparing(String::length) inside Stream.sorted() method
    • As List is sorted in ascending-order, 3rd element in the List will be the third shortest String
  • We will skip first 2 Strings which is the shortest and 2nd shortest Strings using Stream.skip() method
  • Finally, we will print 3rd shortest String to the console

FindThirdShortestStringInListUsingJava8Stream.java

package in.bench.resources.third.shortest.string;

import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class FindThirdShortestStringInListUsingJava8Stream {

	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 ascending-order acc. to String length
		String thirdShortestString = names
				.stream()
				.sorted(Comparator.comparing(String::length))
				.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 Shortest String in List is - "
				+ thirdShortestString);


		// 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 Shortest String in List is - Alice

Execution time - 17753000 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 ascending-order sorting using Stream.limit() method which are the shortest & 2nd shortest and 3rd shortest Strings in the List
  • Using Stream.skip() method, we are skipping first 2 elements and remaining one element in the List is the 3rd shortest String

FindThirdShortestStringInListUsingJava8Stream.java

package in.bench.resources.third.shortest.string;

import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class FindThirdShortestStringInListUsingJava8Stream {

	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 ascending-order acc. to String length
		String thirdShortestString = names
				.stream()
				.sorted(Comparator.comparing(String::length))
				.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 Shortest String in List is - "
				+ thirdShortestString);


		// 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 Shortest String in List is - Alice

Execution time - 16677900 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 – Find Third Longest String in an Arrays or List ?
Java 8 – Find Third Longest String in an Arrays or List or Stream ?