javacomparethree-valued-logic

compare three variables and get the variable with min/max value back (not value)


I have 3 variables with long values timestamp1 timestamp2 timestamp3 and an arraylist timestampList. I want to compare them in an if/else. It could be possible, that the three timestamps have different values, so I want to add the values with the min value to the list. I should also mention, that these timestamps are coming in every 2 minutes.

When the three variables are same, I could simply do

if(timestamp1 == timestamp2 && timestamp2 == timestamp3){
     timestampList.add(timestamp 1);   //since they are the same it doesn't matter which i add to the list
     .
     .
     .
}

now in the else or else if I want to check the three timestamps and get the variable with the min value, not the value itself. Because I need the variable for other variables further in the code. Of course I also want to add the min value to the list too. I can imagine, that I could do more if/else branches in the else like

else{
     if(timestamp1 < timestamp2){
          if(timestamp1 < timestamp3){
               ...
          }else{
               ...
          }
     }
}

but that would be too much and there is certainly a better way.


Solution

  • So I went with the idea of @Amadan and created an array with the timestamps. But after thinking a little bit, I came to the conclusion that it would be possible without getting the variable.

    long[] arrayDo = new long[3];
            arrayDo[0] = eafedo5.getServiceInformation(eafedo5Count).getTimestamp();
            arrayDo[1] = eafedo6.getServiceInformation(eafedo6Count).getTimestamp();
            arrayDo[2] = eafedo7.getServiceInformation(eafedo7Count).getTimestamp();
    

    Then I calculate the minValue of the array.

    long minTimestamp = Math.min(arrayDo[0],Math.min(arrayDo[1],arrayDo[2]));
    

    Then I ask if the timestamps are equal to minValue

    if(!timestamps.contains(minTimestamp)){
                timestamps.add(minTimestamp);
            }
    
            if(eafedo5.getServiceInformation(eafedo5Count).getTimestamp() ==minTimestamp){
                for(CHostNeighbor n : hostNeighborsEafedo5){
                    msgsCountDo += n.getInboundMsgs();
                }
                eafedo5Count--;
            }
            if(eafedo6.getServiceInformation(eafedo6Count).getTimestamp() ==minTimestamp){
                for(CHostNeighbor n : hostNeighborsEafedo6){
                    msgsCountDo += n.getInboundMsgs();
                }
                eafedo6Count--;
            }
            if(eafedo7.getServiceInformation(eafedo7Count).getTimestamp() ==minTimestamp){
                for(CHostNeighbor n : hostNeighborsEafedo7){
                    msgsCountDo += n.getInboundMsgs();
                }
                eafedo7Count--;
            }
    
            msgsDo.add(msgsCountDo);
    

    I mentioned in my question that I need the variable for later purposes. It was because I needed the name of the variable to decrement the count variable of the specific host. (the eafed... are hosts).

    Thanks for all the answers!