Given List Integers Find Max value Java Example
Question: Given the list of integers you have to find maximum value from it. Then input parameter will be Java List:
- MaxValueOfIntFromList.java:
package com.intarrayindex; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class MaxValueOfIntFromList { public static void main(String[] args) { List<Integer> values = new ArrayList<>(); values.add(89); values.add(102); values.add(48); values.add(50); //Using collections API System.out.println("Maximum value from list of int using Collections API: "+Collections.max(values)); //Using own logic System.out.println("Maximum value from list of int using own logic: "+getMaximumValue(values)); } public static Integer getMaximumValue(List<Integer> values){ int maxValue = Integer.MIN_VALUE; for(int i=0; i<values.size(); i++){ if(values.get(i) > maxValue){ maxValue = values.get(i); } } return maxValue; } }
- Output:
Reference: