I am very new to Apex. How can i get the maximum of these variables?

I need to put the max vaule in "varTotalOpRecommendedCrew". And I should find the maximum value among varOpRecommendedfifteenCrew, varOpRecommendedsixteenCrew, varOpRecommendedseventeenCrew, and varOpRecommendedeighteenCrew.

List<Map<String, Object>> mapListDPM = new List<Map<String, Object>>();

            Integer varTotalOpRecommendedCrew = 0;  //
            for (BM_DailyProductivityManagement__c dpml : dailyProductivityManagementList) {
                Integer varOpRecommendedfifteenCrew = (dpml.PT_Fifteen_Rmnd__c == null?0:Integer.valueOf(Math.floor(dpml.PT_Fifteen_Rmnd__c)));
                Integer varOpRecommendedsixteenCrew = (dpml.PT_Sixteen_Rmnd__c == null?0:Integer.valueOf(Math.floor(dpml.PT_Sixteen_Rmnd__c)));
                Integer varOpRecommendedseventeenCrew = (dpml.PT_Seventeen_Rmnd__c == null?0:Integer.valueOf(Math.floor(dpml.PT_Seventeen_Rmnd__c)));
                Integer varOpRecommendedeighteenCrew = (dpml.PT_Eighteen_Rmnd__c == null?0:Integer.valueOf(Math.floor(dpml.PT_Eighteen_Rmnd__c)));   

                varTotalOpRecommendedCrew = varTotalOpRecommendedCrew + ??
                Map<String, Object> mldpmMap = new Map<String, Object>();

                mldpmMap.put('Id15', dpml.Id); 
                mldpmMap.put('remainCount',  varOpRecommendedfifteenCrew);
                mldpmMap.put('Id16', dpml.Id);
                mldpmMap.put('remainCount16', varOpRecommendedsixteenCrew);
                mldpmMap.put('Id17', dpml.Id);
                mldpmMap.put('remainCount17', varOpRecommendedseventeenCrew);
                mldpmMap.put('Id18', dpml.Id);
                mldpmMap.put('remainCount18', varOpRecommendedeighteenCrew);


2 Answers 2


You can simply use Math.max method like this

varTotalOpRecommendedCrew = Math.max(Math.max(varOpRecommendedfifteenCrew ,varOpRecommendedsixteenCrew), Math.max(varOpRecommendedseventeenCrew,varOpRecommendedeighteenCrew ));

This will give you the max of all these four variables.

This solution will work only if you have fixed number of variables, for cases when you are not aware of the number of variables, best solution is to create a List of the desired datatype, sort it and get the max value depending on the sorting.

Integer[] myList = new List<Integer> { 1,4,5,2,10 };
MaxValue = mylist[4];
MinValue = mylist[0];

Not the answer you're looking for? Browse other questions tagged .