Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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
使用if/检查流状态的Java流处理_Java_Api_Lambda_Functional Programming_Java Stream - Fatal编程技术网

使用if/检查流状态的Java流处理

使用if/检查流状态的Java流处理,java,api,lambda,functional-programming,java-stream,Java,Api,Lambda,Functional Programming,Java Stream,给定:客户列表(带供应商和代理字段)、字符串代理、字符串供应商 目标:检查是否有客户支持指定的代理和供应商 我有一个流需要过滤两次(两个值)。 若在第一次过滤后流是空的,我需要检查它并抛出异常。如果它不是空的,我需要通过第二个过滤器处理它(然后再次检查它是否不是空的) 如果可能的话,我想避免将流收集到列表中(并且我不能使用任何匹配或计数方法,因为它们是终端) 目前我的代码看起来像: void checkAgencySupplierMapping(String agency, String sup

给定:客户列表(带供应商和代理字段)、字符串代理、字符串供应商

目标:检查是否有客户支持指定的代理和供应商

我有一个流需要过滤两次(两个值)。 若在第一次过滤后流是空的,我需要检查它并抛出异常。如果它不是空的,我需要通过第二个过滤器处理它(然后再次检查它是否不是空的)

如果可能的话,我想避免将流收集到列表中(并且我不能使用任何匹配或计数方法,因为它们是终端)

目前我的代码看起来像:

void checkAgencySupplierMapping(String agency, String supplier) {
   List<Customers> customersFilteredByAgency = allCustomers.stream()
             .filter(customer -> customer.getAgency().equals(agency))
             .collect(toList());

   if (customersFilteredByAgency.isEmpty()) throw new AgencyNotSupportedException(agency);

   customersFilteredByAgency.stream()
             .filter(customer -> customer.getSupplier().equals(supplier))
             .findFirst().orElseThrow(() -> throw new SupplierNotSupportedException(supplier);
}

是否有Java 8功能可以让我在不终止流的情况下检查流的状态?

下面的代码有点难看,但可以按照您的意愿工作

首先,我们需要统计与之匹配的客户代理数量,然后尝试找到与之匹配的第一个供应商。如果没有匹配项,则抛出异常,但在这里,您将检查原因是否是未找到代理客户,以便抛出正确的例外

AtomicInteger countAgencyMatches = new AtomicInteger(0);

allCustomers.stream()
        .filter(customer -> {
            if (customer.getAgency().equals(agency)) {
                countAgencyMatches.incrementAndGet();
                return true;
            }

            return false;
        })
        .filter(customer -> customer.getSupplier().equals(supplier))
        .findFirst()
        .orElseThrow(() -> {
            if (countAgencyMatches.get() == 0) {
                return new AgencyNotSupportedException(agency);
            }

            return new SupplierNotSupportedException(supplier);
        });

下面的代码有点难看,但可以按照您的意愿工作

首先,我们需要统计与之匹配的客户代理数量,然后尝试找到与之匹配的第一个供应商。如果没有匹配项,则抛出异常,但在这里,您将检查原因是否是未找到代理客户,以便抛出正确的例外

AtomicInteger countAgencyMatches = new AtomicInteger(0);

allCustomers.stream()
        .filter(customer -> {
            if (customer.getAgency().equals(agency)) {
                countAgencyMatches.incrementAndGet();
                return true;
            }

            return false;
        })
        .filter(customer -> customer.getSupplier().equals(supplier))
        .findFirst()
        .orElseThrow(() -> {
            if (countAgencyMatches.get() == 0) {
                return new AgencyNotSupportedException(agency);
            }

            return new SupplierNotSupportedException(supplier);
        });
您不能检查该选项。因此,您的第一个代码块是最简单、最简单的。因此,您的第一个代码块非常简单。请参阅