Running if condition after all three items are updated

helen :

I have a list which is supposed to receive values from several hosts periodically and do some operations on these values.

List<Integer> loadList = new ArrayList<>(3);
if ((dstIp.toString().equals("10.0.0.1")
                        && udp.getPayload() != null) {

                    loadList.add(0, intLoad1);
                }
                else if ((dstIp.toString().equals("10.0.0.2") 
                        && udp.getPayload() != null) {

                    loadList.add(1, intLoad2);
                }
                else if ((dstIp.toString().equals("10.0.0.3") 
                        && udp.getPayload() != null) {

                    loadList.add(2, intLoad3);
                }

                if (!loadList.isEmpty() && !loadList.contains(null)) {
                    int sum = loadList.stream().mapToInt(Integer::intValue).sum();
                    System.out.println("---- Sum: "+sum);
                    double averageLoad = ((double) sum) / loadList.size();
                }

In my code the 4th if condition (I mean if (!loadList.isEmpty() && !loadList.contains(null))) will run every time it receives a new value, but I'm trying to change it so that it will run after the end of each period (when all the three elements in the list are updated). I mean I want it to run only when all the three load value are received (so that I will find the average value of the newly received values).

I searched for a solution but I didn't find anything. Is it possible?

Centos :

Yo can add three boolean fields and use them in the last if statement:

boolean first = false;
boolean second= false;
boolean third = false;
List<Integer> loadList = new ArrayList<>(3);
if ((dstIp.toString().equals("10.0.0.1") && udp.getPayload() != null) {
    loadList.add(0, intLoad1);
    first = true;
} else if ((dstIp.toString().equals("10.0.0.2") && udp.getPayload() != null) {
    loadList.add(1, intLoad2);
    second = true;
} else if ((dstIp.toString().equals("10.0.0.3") && udp.getPayload() != null) {
    loadList.add(2, intLoad3);
    third = true;
}

if (!loadList.isEmpty() && !loadList.contains(null) && first && second && third) {
    int sum = loadList.stream().mapToInt(Integer::intValue).sum();
    System.out.println("---- Sum: "+sum);
    double averageLoad = ((double) sum) / loadList.size();
    first = false;
    second= false;
    third = false;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=135832&siteId=1