[英]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.