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

1.2k Views Asked by At

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.

4

There are 4 best solutions below

0
On BEST ANSWER

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!

0
On

Try this:

    long timestamp1 = 1;
    long timestamp2 = 2;
    long timestamp3 = 3;
    long result = LongStream.of(timestamp1, timestamp2, timestamp3)
            .min()
            .getAsLong();
0
On

You can not really get a "pointer" to a variable in Java, as you could in C. The closest thing would be using a mutable type instead, so instead of assigning a new value to the variable you can modify an attribute of the existing instance, and the change will be reflected anywhere else in your code where you have a reference to that instance.

For example, you could wrap your long timestamps into AtomicLong instances:

// wrap in mutable AtomicLong instances
AtomicLong timestamp1 = new AtomicLong(123);
AtomicLong timestamp2 = new AtomicLong(456);
AtomicLong timestamp3 = new AtomicLong(789);

// get minimum or maximum, using streams or any other way
AtomicLong minTimeStamp = Stream.of(timestamp1, timestamp2, timestamp3)
        .min(Comparator.comparing(AtomicLong::get)).get();

// modify value
System.out.println(minTimeStamp); // 123
timestamp1.set(1000);             // change original variable
System.out.println(minTimeStamp); // 1000
0
On

Another way to keep a variable to a certain value would be to use Optional<Long> as @tobias_k mentioned in his comment. An alternative to Stream would be to use a TreeSet. When creating the TreeSet we provide the Function to compare the elements. After all timestamps has been added, we can call TreeSet.first() to get the minimum element.

List<Optional<Long>> timestamps = Arrays.asList(Optional.of(1234l), Optional.of(2345l), Optional.of(1234l));
TreeSet<Optional<Long>> setOfTimestamps = new TreeSet<>(Comparator.comparing(Optional::get));
setOfTimestamps.addAll(timestamps);

Optional<Long> min = setOfTimestamps.first();
System.out.println(min.get());