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

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

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

# 1. Finding Second Largest number in an Arrays

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

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

## 1.1 Using Stream.skip() method

### FindSecondLargestNumberInAnArraysUsingJava8Stream.java

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

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

public class FindSecondLargestNumberInAnArraysUsingJava8Stream {

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 descending-order and get 2nd largest element
int secondLargestNumber = Arrays
.stream(numbers)
.boxed()
.sorted(Comparator.reverseOrder())
.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 an Arrays is - "
+ secondLargestNumber);

// 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 largest number in an Arrays is - 11

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

### FindSecondLargestNumberInAnArraysUsingJava8Stream.java

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

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

public class FindSecondLargestNumberInAnArraysUsingJava8Stream {

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 descending-order and get 2nd largest element
int secondLargestNumber = Arrays
.stream(numbers)
.boxed()
.sorted(Comparator.reverseOrder())
.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 an Arrays is - "
+ secondLargestNumber);

// 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 largest number in an Arrays is - 11

Execution time - 31000000 ns
```

# 2. Finding Second Largest number in List or ArrayList

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

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

## 2.1 Using Stream.skip() method

### FindSecondLargestNumberInListUsingJava8Stream.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 FindSecondLargestNumberInListUsingJava8Stream {

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 descending-order and get 2nd largest element
int secondLargestNumber = numbers
.stream()
.sorted(Comparator.reverseOrder())
.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, 9, 11, 2, 8, 21, 1]

Second largest number in List is - 11

Execution time - 16000000 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 largest & 2nd largest 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 largest number

### FindSecondLargestNumberInListUsingJava8Stream.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 FindSecondLargestNumberInListUsingJava8Stream {

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 descending-order and get 2nd largest element
int secondLargestNumber = numbers
.stream()
.sorted(Comparator.reverseOrder())
.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, 9, 11, 2, 8, 21, 1]

Second largest number in List is - 11

Execution time - 16000000 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

## References:

Happy Coding !!
Happy Learning !!