Java List Average getAverageInterval(List tapTimestamps, int n)

Here you can find the source of getAverageInterval(List tapTimestamps, int n)

Description

Calculates the average difference between the last n timestamp intervals of the input array.

License

Open Source License

Parameter

Parameter Description
tapTimestamps the list of timestamps
n the number of timestamp intervals we'd like to use for the calculation.

Return

the average interval between the considered values

Declaration

public static long getAverageInterval(List<Long> tapTimestamps, int n) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.List;

public class Main {
    /**/*w ww  .  j  av a 2  s .c  om*/
     * Calculates the average difference between the last n timestamp intervals of the input array. The assumption is that the list of timestamps is increasing.
     * 
     * @param tapTimestamps
     *          the list of timestamps
     * @param n
     *          the number of timestamp intervals we'd like to use for the calculation.
     * @return the average interval between the considered values
     */
    public static long getAverageInterval(List<Long> tapTimestamps, int n) {

        long result = 0;
        int start = tapTimestamps.size() - (n + 1);

        if (start < 0) {
            start = 0;
            n = tapTimestamps.size() - 1;
        }

        if (n == 0) {
            return 0;
        }

        for (int i = start; i < tapTimestamps.size() - 1; i++) {
            result += Math.abs(tapTimestamps.get(i + 1) - tapTimestamps.get(i));
        }

        return (result / n);
    }
}

Related

  1. getAverage(List values)
  2. getAverage(List values)
  3. getAverage(List numbers)
  4. getAverageColour(List colours)
  5. getAverageDuration(List durations)
  6. getStandardDeviation(List longList, long average, long devByZero)