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 :
- First, get Stream from Arrays using Arrays.stream() method
- Convert primitive integers into Integer objects using Stream.boxed() method
- Sort Integer objects in ascending-order using Comparator.naturalOrder() inside Stream.sorted() method
- As integer Arrays sorted in ascending-order, 2nd element in the Arrays will be the second smallest number
- We will skip first number which is the smallest number using Stream.skip() method
- Stream.findFirst() method will return 2nd smallest number in the Arrays
- Finally, we will print 2nd smallest number to the console
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 :
- First, get Stream from List using List.stream() method
- Sort Integer objects in ascending-order using Comparator.naturalOrder() inside Stream.sorted() method
- As integer List is sorted in ascending-order, 2nd element in the List or ArrayList will be the second smallest number
- We will skip first number which is the smallest number using Stream.skip() method
- Stream.findFirst() method will return 2nd smallest number in the List
- Finally, we will print 2nd smallest number to the console
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:
- 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 !!