如何根据列表中的值使用流过滤列表列表?
How to filter list of lists using streams on the basis of a value in a list?
我有一个List<List<Double>>
。我想根据索引过滤行,即如果索引 4 处的元素值小于 0.2,则跳过该行?结果 List<List<Double>>
应该小于或等于输入的。
您可以使用 Stream.filter
。请注意,您必须 select 您想要获取的行,而不是您想要跳过的行:
List<List<Double>> input = ...;
List<List<Double>> result = input.stream()
.filter(row -> row.get(4) >= 0.2)
.collect(Collectors.toList());
Stream API 的替代方法是使用 Collection.removeIf
:
进行就地修改
List<List<Double>> input = ...;
input.removeIf(row -> row.get(4) < 0.2);
您可以使用 lambda 表达式,这很好,但是对于更可重用的东西,您可以考虑使用您自己的 Predicate
,例如
public class SubElementPredict implements Predicate<List<Double>> {
private int index;
private double value;
public SubElementPredict(int index, double value) {
this.index = index;
this.value = value;
}
@Override
public boolean test(List<Double> t) {
return value < t.get(index);
}
}
然后你可以做类似...
List<List<Double>> values = new ArrayList<>(25);
values.add(Arrays.asList(new Double[]{1d, 2d, 3d, 4d, 5d}));
values.add(Arrays.asList(new Double[]{6d, 7d, 8d, 9d, 10d}));
values.add(Arrays.asList(new Double[]{11d, 12d, 13d, 14d, 15d}));
int index = 2;
double value = 8d;
List<List<Double>> filtered = values
.stream()
.filter(new SubElementPredict(index, value))
.collect(Collectors.toList());
for (List<Double> sub : filtered) {
System.out.println(sub);
}
输出
[11.0, 12.0, 13.0, 14.0, 15.0]
现在,如果您想进行更多冒险,您甚至可以做类似...
public class SubElementPredict<V extends Comparable> implements Predicate<List<V>> {
private int index;
private V value;
public SubElementPredict(int index, V value) {
this.index = index;
this.value = value;
}
@Override
public boolean test(List<V> t) {
return value.compareTo(t.get(index)) <= 0;
}
}
现在,您可以使用 Comparable
个值中的任意 List
个
我有一个List<List<Double>>
。我想根据索引过滤行,即如果索引 4 处的元素值小于 0.2,则跳过该行?结果 List<List<Double>>
应该小于或等于输入的。
您可以使用 Stream.filter
。请注意,您必须 select 您想要获取的行,而不是您想要跳过的行:
List<List<Double>> input = ...;
List<List<Double>> result = input.stream()
.filter(row -> row.get(4) >= 0.2)
.collect(Collectors.toList());
Stream API 的替代方法是使用 Collection.removeIf
:
List<List<Double>> input = ...;
input.removeIf(row -> row.get(4) < 0.2);
您可以使用 lambda 表达式,这很好,但是对于更可重用的东西,您可以考虑使用您自己的 Predicate
,例如
public class SubElementPredict implements Predicate<List<Double>> {
private int index;
private double value;
public SubElementPredict(int index, double value) {
this.index = index;
this.value = value;
}
@Override
public boolean test(List<Double> t) {
return value < t.get(index);
}
}
然后你可以做类似...
List<List<Double>> values = new ArrayList<>(25);
values.add(Arrays.asList(new Double[]{1d, 2d, 3d, 4d, 5d}));
values.add(Arrays.asList(new Double[]{6d, 7d, 8d, 9d, 10d}));
values.add(Arrays.asList(new Double[]{11d, 12d, 13d, 14d, 15d}));
int index = 2;
double value = 8d;
List<List<Double>> filtered = values
.stream()
.filter(new SubElementPredict(index, value))
.collect(Collectors.toList());
for (List<Double> sub : filtered) {
System.out.println(sub);
}
输出
[11.0, 12.0, 13.0, 14.0, 15.0]
现在,如果您想进行更多冒险,您甚至可以做类似...
public class SubElementPredict<V extends Comparable> implements Predicate<List<V>> {
private int index;
private V value;
public SubElementPredict(int index, V value) {
this.index = index;
this.value = value;
}
@Override
public boolean test(List<V> t) {
return value.compareTo(t.get(index)) <= 0;
}
}
现在,您可以使用 Comparable
个值中的任意 List
个