# Find the second-highest number in an ArrayList in Java

In this tutorial, we are going to learn how to find the second-highest number in an ArrayList in Java.

ArrayList is present in java.util package and it provides a dynamic approach for the Arrays. We can perform various operations such as modifying, storing, removing, manipulating Arrays whenever and wherever we want.

Our task is to find the second-highest number in an ArrayList, by simply sorting the numbers in an ArrayList we can achieve our goal. Sort the numbers in ascending order and return the second number from the end.

We can find the second-highest number in an ArrayList by following simple steps as follows:

• Import java.util package for ArrayList.
• Create an object of ArrayList.
`ArrayList<Integer> arrayList = new ArrayList<Integer>();`
• Sort the numbers in ArrayList using Collections.sort(ArrayList) method.
`Collections.sort(arrayList);`
• Return the second-highest number from the sorted ArrayList.
`arrayList.get(arrayList.size()-2);`
• Finally, print the number.

Following is the Java code for our task:

```//import package for ArrayList
import java.util.*;
public class ArrayList {
public static void main(String args[]){
int temp = 0;
//create object of ArrayList
ArrayList<Integer> arrayList = new ArrayList<Integer>();
//sort numbers in an ArrayList
Collections.sort(arrayList);
//return second highest number from sorted ArrayList
temp=arrayList.get(arrayList.size()-2);
System.out.println("Second highest number");
System.out.println(temp);
}
}
```

Output:

```Second highest number
8```

This is how we can find the second-highest number in an ArrayList in Java. I hope you find this tutorial useful.
{"ft_lang_label":"__label__en","ft_lang_prob":0.71503067,"math_prob":0.7550253,"size":1916,"snap":"2021-43-2021-49","text_gpt3_token_len":394,"char_repetition_ratio":0.24320084,"word_repetition_ratio":0.09022556,"special_character_ratio":0.2197286,"punctuation_ratio":0.16666667,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99030495,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-16T02:28:35Z\",\"WARC-Record-ID\":\"<urn:uuid:0fc59208-f835-4ef9-8c7a-7354d0d1fa7e>\",\"Content-Length\":\"23218\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7ecf7802-60b9-4164-9f47-162b183d01b5>\",\"WARC-Concurrent-To\":\"<urn:uuid:ed31cbf3-34ba-443b-81ca-d29ca4c59243>\",\"WARC-IP-Address\":\"173.255.247.200\",\"WARC-Target-URI\":\"https://www.codespeedy.com/find-the-second-highest-number-in-an-arraylist-in-java/\",\"WARC-Payload-Digest\":\"sha1:JLBN5QYQKRO4RA7IC4VFLWOB6LHROYFP\",\"WARC-Block-Digest\":\"sha1:D3JACNIAIBYPFNUY4ONZFD6YJ6JOJT3N\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323583408.93_warc_CC-MAIN-20211016013436-20211016043436-00591.warc.gz\"}"}