Here we have taken 3 elements here to find the Top 3 elements from the list.
public class Find3rdHighest {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add(12);
list.add(87);
list.add(54);
list.add(64);
int max = list.get(0);
int max1 = list.get(0);
//Max2 will be the 3rd highest value
int max2 = list.get(0);
for (Integer integer : list) {
if (integer> max) {
max1 = max; // Need to set this for comparing in next if else it will fail
max = integer;
}
if ((integer > max1) && (max > integer)) {
max2 = max1;// Need to set this for comparing in next if else it will fail
max1 = integer;
}
if ((integer > max2) && (max1 > integer)) {
max2 = integer;
}
}
System.out.println(max2);
}
}
ConversionConversion EmoticonEmoticon