1

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.

NECben067
  • 427
  • 1
  • 4
  • 20
  • What is the type of your timestamps? Is it `int`, `long` or anything else? – Thomas Fritsch Sep 04 '18 at 09:30
  • 2
    "get the **variable** with the min value, not the value itself" - Not really possible in Java. Instead, use a three-element array, and figure out the index of the smallest value; then you can use the combination of the array and the index as you would a variable reference. – Amadan Sep 04 '18 at 09:30
  • Instead of `List` you should use `Set`, because `Set` automatically removes duplicates. – Thomas Fritsch Sep 04 '18 at 09:34
  • Or use some sort of wrapper for the timestamps, e.g. an `Optional` or `AtomicLong`, then you can get the wrapper with the min/max value and update the value later on. – tobias_k Sep 04 '18 at 09:38

4 Answers4

0

Try this:

    long timestamp1 = 1;
    long timestamp2 = 2;
    long timestamp3 = 3;
    long result = LongStream.of(timestamp1, timestamp2, timestamp3)
            .min()
            .getAsLong();
talex
  • 17,973
  • 3
  • 29
  • 66
0

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
tobias_k
  • 81,265
  • 12
  • 120
  • 179
0

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());
LuCio
  • 5,055
  • 2
  • 18
  • 34
0

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!

NECben067
  • 427
  • 1
  • 4
  • 20