Java 8 – Find Second Smallest number in an Arrays or List or Stream ?

In this article, we will discuss how to find second smallest number in an Arrays and List using Java 8 Stream

Read Java – Find Second Smallest number in an Arrays or List ? for finding second smallest number without using Java 8 syntax

1. Finding Second Smallest number in an Arrays :

We will follow below 2 approaches to get 2nd Smallest number in an Arrays

  • Using Stream.skip() method
  • Using Stream.limit() & Stream.skip() methods

1.1 Using Stream.skip() method :

FindSecondSmallestNumberInAnArraysUsingJava8Stream.java

package in.bench.resources.second.smallest.number;

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

public class FindSecondSmallestNumberInAnArraysUsingJava8Stream {

	public static void main(String[] args) {

		// random numbers
		int[] numbers = {5, 9, 11, 2, 8, 21, 1};


		// print to console
		System.out.println("Numbers in an Arrays : " 
				+ Arrays.toString(numbers));


		// Execution - start time
		LocalTime startTime = LocalTime.now();


		// sort in ascending-order and get 2nd smallest element
		int secondSmallestNumber = Arrays
				.stream(numbers)
				.boxed()
				.sorted(Comparator.naturalOrder())
				.skip(1)
				.findFirst()
				.get();


		// Execution - end time
		LocalTime endTime = LocalTime.now();


		// find difference
		Duration duration = Duration.between(startTime, endTime);
		long differenceInNano = duration.getNano();


		// print sum to console
		System.out.println("\nSecond smallest number in an Arrays is - "
				+ secondSmallestNumber);


		// print execution time in Nano seconds
		System.out.println("\nExecution time - "
				+ differenceInNano + " ns");
	}
}

Output:

Numbers in an Arrays : [5, 9, 11, 2, 8, 21, 1]

Second smallest number in an Arrays is - 2

Execution time - 63000000 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 2 numbers after sorting using Stream.limit() method which are the smallest & 2nd smallest numbers in the Arrays
  • Using Stream.skip() method, we are skipping 1st element and remaining one element in the Arrays is the 2nd smallest number

FindSecondSmallestNumberInAnArraysUsingJava8Stream.java

package in.bench.resources.second.smallest.number;

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

public class FindSecondSmallestNumberInAnArraysUsingJava8Stream {

	public static void main(String[] args) {

		// random numbers
		int[] numbers = {5, 9, 11, 2, 8, 21, 1};


		// print to console
		System.out.println("Numbers in an Arrays : " 
				+ Arrays.toString(numbers));


		// Execution - start time
		LocalTime startTime = LocalTime.now();


		// sort in ascending-order and get 2nd smallest element
		int secondSmallestNumber = Arrays
				.stream(numbers)
				.boxed()
				.sorted(Comparator.naturalOrder())
				.limit(2)
				.skip(1)
				.findFirst()
				.get();


		// Execution - end time
		LocalTime endTime = LocalTime.now();


		// find difference
		Duration duration = Duration.between(startTime, endTime);
		long differenceInNano = duration.getNano();


		// print sum to console
		System.out.println("\nSecond smallest number in an Arrays is - "
				+ secondSmallestNumber);


		// print execution time in Nano seconds
		System.out.println("\nExecution time - "
				+ differenceInNano + " ns");
	}
}

Output:

Numbers in an Arrays : [5, 9, 11, 2, 8, 21, 1]

Second smallest number in an Arrays is - 2

Execution time - 16000000 ns

2. Finding Second Smallest number in List or ArrayList :

We will follow below 2 approaches to get 2nd Smallest number in List or ArrayList

  • Using Stream.skip() method
  • Using Stream.limit() & Stream.skip() methods

2.1 Using Stream.skip() method :

FindSecondSmallestNumberInListUsingJava8Stream.java

package in.bench.resources.second.smallest.number;

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

public class FindSecondSmallestNumberInListUsingJava8Stream {

	public static void main(String[] args) {

		// random numbers
		List<Integer> numbers = Arrays.asList(5, 9, 11, 2, 8, 21, 1);


		// print to console
		System.out.println("Original Integer List : " + numbers);


		// Execution - start time
		LocalTime startTime = LocalTime.now();


		// sort in ascending-order and get 2nd smallest element
		int secondSmallestNumber = numbers
				.stream()
				.sorted(Comparator.naturalOrder())
				.skip(1)
				.findFirst()
				.get();


		// Execution - end time
		LocalTime endTime = LocalTime.now();


		// find difference
		Duration duration = Duration.between(startTime, endTime);
		long differenceInNano = duration.getNano();


		// print sum to console
		System.out.println("\nSecond Smallest number in List is - "
				+ secondSmallestNumber);


		// print execution time in Nano seconds
		System.out.println("\nExecution time - "
				+ differenceInNano + " ns");
	}
}

Output:

Original Integer List : [5, 9, 11, 2, 8, 21, 1]

Second Smallest number in List is - 2

Execution time - 15000000 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 2 numbers after sorting using Stream.limit() method which are the smallest & 2nd smallest numbers in the List or ArrayList
  • Using Stream.skip() method, we are skipping 1st element and remaining one element in the List is the 2nd smallest number

FindSecondSmallestNumberInListUsingJava8Stream.java

package in.bench.resources.second.smallest.number;

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

public class FindSecondSmallestNumberInListUsingJava8Stream {

	public static void main(String[] args) {

		// random numbers
		List<Integer> numbers = Arrays.asList(5, 9, 11, 2, 8, 21, 1);


		// print to console
		System.out.println("Original Integer List : " + numbers);


		// Execution - start time
		LocalTime startTime = LocalTime.now();


		// sort in ascending-order and get 2nd smallest element
		int secondSmallestNumber = numbers
				.stream()
				.sorted(Comparator.naturalOrder())
				.limit(2)
				.skip(1)
				.findFirst()
				.get();


		// Execution - end time
		LocalTime endTime = LocalTime.now();


		// find difference
		Duration duration = Duration.between(startTime, endTime);
		long differenceInNano = duration.getNano();


		// print sum to console
		System.out.println("\nSecond Smallest number in List is - "
				+ secondSmallestNumber);


		// print execution time in Nano seconds
		System.out.println("\nExecution time - "
				+ differenceInNano + " ns");
	}
}

Output:

Original Integer List : [5, 9, 11, 2, 8, 21, 1]

Second Smallest number in List is - 2

Execution time - 31000000 ns

3. Find 2nd Smallest number from Arrays/List containing duplicates :

In the below illustration, we will find 2nd smallest number from ArrayList or Arrays containing duplicates

  • Below list contains 7 integers where number 1 & 2 is repeated twice
  • Note: 1 is the smallest number & 2 is the 2nd smallest number

FindSecondSmallestNumber.java

package in.bench.resources.second.largest.number;

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

public class FindSecondSmallestNumber {

	public static void main(String[] args) {

		// random numbers
		List<Integer> numbers = Arrays.asList(5, 1, 5, 2, 2, 9, 1);


		// print to console
		System.out.println("Original Integer List : " + numbers);


		// Execution - start time
		LocalTime startTime = LocalTime.now();


		// sort in ascending-order and get 2nd largest element
		int secondLargestNumber = numbers
				.stream()
				.distinct()
				.sorted(Comparator.naturalOrder())
				.limit(2)
				.skip(1)
				.findFirst()
				.get();


		// Execution - end time
		LocalTime endTime = LocalTime.now();


		// find difference
		Duration duration = Duration.between(startTime, endTime);
		long differenceInNano = duration.getNano();


		// print sum to console
		System.out.println("\nSecond largest number in List is - "
				+ secondLargestNumber);


		// print execution time in Nano seconds
		System.out.println("\nExecution time - "
				+ differenceInNano + " ns");


	}
}

Output :

Original Integer List : [5, 1, 5, 2, 2, 9, 1]

Second largest number in List is - 2

Execution time - 19255600 ns

4. 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 – Remove input-ted Character from a given String
Java 8 – Find Second Largest number in an Arrays or List or Stream ?