简体   繁体   English

检查Java列表中是否有大于阈值的连续点

[英]Check if there are consecutive points in Java list greater than a threshold

I have a list List<Double> representing latency values collected from server metrics.我有一个列表List<Double>表示从服务器指标收集的延迟值。 I want to check if there are 3 consecutive values are greater than a given threshold.我想检查是否有 3 个连续的值大于给定的阈值。

eg threshold = 20例如阈值 = 20

list 1: [15.121, 15.245, 20.883, 20.993, 15.378, 15.447, 15.839, 15.023] should return false because there are only two values 20.883, 20.993 that are greater than 20.列表 1: [15.121, 15.245, 20.883, 20.993, 15.378, 15.447, 15.839, 15.023]应该返回 false 因为只有两个值20.883, 20.993大于 2.

list 2: [15.121, 15.245, 20.883, 20.993, 15.378, 15.447, 20.193, 15.023] should return false because there are only three values greater than 20 but they are not consecutive.列表 2: [15.121, 15.245, 20.883, 20.993, 15.378, 15.447, 20.193, 15.023]应该返回 false 因为只有三个值大于 20 但它们不连续。

list 3: [15.121, 15.245, 20.883, 20.993, 20.193, 15.378, 15.447, 15.023] should return true because there are three consecutive values 20.883, 20.993, 20.193 greater than 20.列表 3: [15.121, 15.245, 20.883, 20.993, 20.193, 15.378, 15.447, 15.023]应该返回 true 因为有三个连续的值20.883, 20.993, 20.193大于 20.

I could do a loop with index to check on list.get(i-1), list.get(i), and list.get(i+1).我可以做一个带索引的循环来检查 list.get(i-1)、list.get(i) 和 list.get(i+1)。

public boolean isAboveThreshold(List<Double> list, Double threshold) {
    // CONSECUTIVE_NUMBER = 3
    if (list.size() < CONSECUTIVE_NUMBER) {
        return false;
    }

    return !IntStream.range(0, list.size() - 2)
        .filter(i -> list.get(i) > threshold && list.get(i + 1) > threshold && list.get(i + 2) > thread)
        .collect(Collectors.toList())
        .isEmpty();
}

Just wondering is there a more efficient way to do it?只是想知道有没有更有效的方法来做到这一点?


Updated with anyMatch base on Andy Turner 's comment.根据安迪·特纳( Andy Turner ) 的评论更新了anyMatch

public boolean isAboveThreshold(List<Double> values, Double threshold, int consecutiveNumber) {
    if (values.size() < consecutiveNumber) {
        return false;
    }

    return IntStream
        .range(0, values.size() - consecutiveNumber + 1)
        .anyMatch(index -> 
            IntStream.range(index, index + consecutiveNumber)
                .allMatch(i -> values.get(i) > threshold)
        );
}

It's easiest just to do it with an enhanced for loop, keeping a count of the number of elements you have seen in a contiguous run:最简单的方法是使用增强的 for 循环来完成它,记录您在连续运行中看到的元素数量:

int count = 0;
for (double d : list) {
  if (d >= threshold) {
    // Increment the counter, value was big enough.
    ++count;
    if (count >= 3) {
      return true;
    }
  } else {
    // Reset the counter, value too small.
    count = 0;
  }
}
return false;

I have a list List<Double> representing latency values collected from server metrics.我有一个列表List<Double>代表从服务器指标收集的延迟值。 I want to check if there are 3 consecutive values are greater than a given threshold.我想检查是否有3个连续的值大于给定的阈值。

eg threshold = 20例如,阈值= 20

list 1: [15.121, 15.245, 20.883, 20.993, 15.378, 15.447, 15.839, 15.023] should return false because there are only two values 20.883, 20.993 that are greater than 20.清单1: [15.121, 15.245, 20.883, 20.993, 15.378, 15.447, 15.839, 15.023]应该返回false,因为只有两个值20.883, 20.993大于20。

list 2: [15.121, 15.245, 20.883, 20.993, 15.378, 15.447, 20.193, 15.023] should return false because there are only three values greater than 20 but they are not consecutive.清单2: [15.121, 15.245, 20.883, 20.993, 15.378, 15.447, 20.193, 15.023]应该返回false,因为只有三个大于20的值,但它们不是连续的。

list 3: [15.121, 15.245, 20.883, 20.993, 20.193, 15.378, 15.447, 15.023] should return true because there are three consecutive values 20.883, 20.993, 20.193 greater than 20.清单3: [15.121, 15.245, 20.883, 20.993, 20.193, 15.378, 15.447, 15.023]应该返回true,因为三个连续的值20.883, 20.993, 20.193大于20。

I could do a loop with index to check on list.get(i-1), list.get(i), and list.get(i+1).我可以做一个循环与索引来检查list.get(i-1),list.get(i)和list.get(i + 1)。

public boolean isAboveThreshold(List<Double> list, Double threshold) {
    // CONSECUTIVE_NUMBER = 3
    if (list.size() < CONSECUTIVE_NUMBER) {
        return false;
    }

    return !IntStream.range(0, list.size() - 2)
        .filter(i -> list.get(i) > threshold && list.get(i + 1) > threshold && list.get(i + 2) > thread)
        .collect(Collectors.toList())
        .isEmpty();
}

Just wondering is there a more efficient way to do it?只是想知道是否有更有效的方法?


Updated with anyMatch base on Andy Turner 's comment.根据Andy Turner的评论使用anyMatch更新。

public boolean isAboveThreshold(List<Double> values, Double threshold, int consecutiveNumber) {
    if (values.size() < consecutiveNumber) {
        return false;
    }

    return IntStream
        .range(0, values.size() - consecutiveNumber + 1)
        .anyMatch(index -> 
            IntStream.range(index, index + consecutiveNumber)
                .allMatch(i -> values.get(i) > threshold)
        );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查Java中的值是否大于零 - Check if value is greater than zero in Java Java - 如果条件检查整数数组列表是否包含大于特定值的元素 - Java - If condition to check if an Integer array-list contains element which is greater than particular value 建立分数严格高于给定阈值的申请人姓名列表 - Build a list of the names of applicants who have scores strictly greater than the given threshold 如何检查列表中的数字是否大于并延续到列表中的下一个数字 - How to check if number in a list is greater than and in continuation to next number in list 检查Java版本是否大于Java中的某个迭代? - Check if a Java version is greater than a certain iteration in Java? Selenium2 Java-检查页面上的值是否大于X - Selenium2 Java - Check if value on page is greater than X 有没有一种更快的方法来检查列表中的某个项目是否大于,小于或等于某个数字? - Is there a faster way to check if an item in a list if it is greater than, less than, equal to a certain number? 检查大于或小于wiremock - check greater than or less than in wiremock Sqlite查询检查 - 小于和大于 - Sqlite query check - less than and greater than Java并发比连续慢吗? - Java concurrent is slower than consecutive?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM