Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何过滤具有多个参数的流数据?_Java_Java Stream - Fatal编程技术网

Java 如何过滤具有多个参数的流数据?

Java 如何过滤具有多个参数的流数据?,java,java-stream,Java,Java Stream,我有一个来自存储在S3中的JSON文件的源代码列表。我需要在列表的几个字段上提供搜索过滤器。我已经创建了一个Builder类,其中包含了可以过滤数据的所有必需字段 现在我想使用Java streams,并根据用户提供的参数对数据应用过滤器。用户可根据需要提供全部6个参数或只传递2或3个参数。例如,用户只提供6个参数中的2个,我应该能够根据给定的2个参数过滤数据,并忽略值为null的4个参数 public class ListOfFields { private String countr

我有一个来自存储在S3中的JSON文件的源代码列表。我需要在列表的几个字段上提供搜索过滤器。我已经创建了一个Builder类,其中包含了可以过滤数据的所有必需字段

现在我想使用Java streams,并根据用户提供的参数对数据应用过滤器。用户可根据需要提供全部6个参数或只传递2或3个参数。例如,用户只提供6个参数中的2个,我应该能够根据给定的2个参数过滤数据,并忽略值为null的4个参数

public class ListOfFields {
    private String country;
    @JsonProperty("product")
    private String product;
    private String transactionId;
    private String sourceBy;
    private String category;
    private String specialTag;
    private String organicTag;
    private String successTag;
    private String speedTag;
    private String foodTag;
    private String costPrice;
    private String fruitType;
    private String fruitVendorId;    
} 
下面是生成器类

public class ProductClass {
    @Builder.Default
    private String country = 'UnitedStates';
    private String sourceBy;
    private Boolean specialTag;
    private String category;
    private Boolean priceTag;
    private String fruitType;
}  

下面的方法,用户可以通过该方法调用以获取产品

public String getProduct(ProductClass productQuery) {      
    List<ListOfFields> result = listOfFields // (the declaration is made before in the class as private List<ListOfFields> listOfFields;)
            .stream().filter(productQuery::match)
            .collect(Collectors.toList());
公共字符串getProduct(ProductClass productQuery){
List result=LISTOFIELDS//(之前在类中声明为private List LISTOFIELDS;)
.stream().filter(productQuery::match)
.collect(Collectors.toList());

如何将动态谓词作为上面的匹配函数?我应该能够根据用户提供的参数进行筛选,无论是1、2、3或4……并且不使用用户未在筛选条件中传递任何值的参数。

您可以对每个参数使用常规if语句,并应用返回Str的筛选器eam

Stream< ListOfFields> stream = listOfFields.stream();
if(first parameter exists) {
    stream = stream.filter(predicate for first parameter);
}
if(second parameter exists) {
    stream = stream.filter(predicate for second parameter);
}
...
List< ListOfFields> result = stream.collect(Collectors.toList());
StreamStream=listofeelds.Stream();
if(第一个参数存在){
stream=stream.filter(第一个参数的谓词);
}
if(第二个参数存在){
stream=stream.filter(第二个参数的谓词);
}
...
Listresult=stream.collect(Collectors.toList());

ProductClass
作为输入,并将
null
值作为缺少键的值。您可以使用接受实际查询参数的方法定义
谓词的列表

private static List<Predicate<ListOfFields>> predicates(ProductClass product) {
    return List.of(
            lof -> product.priceTag != null && product.priceTag,
            lof -> product.specialTag != null && product.specialTag,
            lof -> product.country != null && product.country.equals(lof.country),
            lof -> product.sourceBy != null && product.sourceBy.equals(lof.sourceBy),
            lof -> product.category != null && product.category.equals(lof.category),
            lof -> product.fruitType != null && product.fruitType.equals(lof.fruitType)
    );
}
现在,当
stream
ing类型
listofelds
--

List result=listofelds.stream()
.filter(匹配所有(谓词(productQuery)))
.collect(Collectors.toList());
private static Predicate<ListOfFields> matchAll(List<Predicate<ListOfFields>> queries) {
    return queries.stream()
            .reduce(Predicate::and)
            .orElse(p -> true);
}
List<ListOfFields> result = listOfFields.stream()
        .filter(matchAll(predicates(productQuery)))
        .collect(Collectors.toList());