简体   繁体   English

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

[英]Java Stream processing with if / checking stream state

Given: list of Customers (with Supplier and Agency fields), String agency, String supplier.给定:客户列表(带有供应商和代理字段)、字符串代理、字符串供应商。

Goal: check if any customer supports given agency AND given supplier.目标:检查是否有任何客户支持给定的代理机构和给定的供应商。

I have a stream that needs to be filtered twice (by two values).我有一个需要过滤两次(按两个值)的流。 If stream is empty after first filtering, I need to check it and throw exception.如果第一次过滤后流为空,我需要检查它并抛出异常。 If it's not empty, I need to process it through second filter (and then check again if it's not empty).如果它不为空,我需要通过第二个过滤器处理它(然后再次检查它是否为空)。

I want to avoid collecting stream to lists if it's possible (and I can't use anyMatch or count methods because they are terminal)如果可能的话,我想避免将流收集到列表中(并且我不能使用 anyMatch 或 count 方法,因为它们是终端)

Currently my code look's like:目前我的代码看起来像:

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);
}

In this example I skipped some technical details about filtering (eg. parsing Supplier to String).在这个例子中,我跳过了一些关于过滤的技术细节(例如,将供应商解析为字符串)。

And I want to achieve something like this:我想实现这样的目标:

void checkAgencySupplierMapping(String agency, String supplier) {
    allCustomers.stream()
       .filter(customer -> customer.getAgency().equals(agency))
       .ifEmpty( () -> throw new AgencyNotSupportedException(agency) )
       .filter( customer -> customer.getSupplier().equals(supplier)
       .ifEmpty( () -> throw new SupplierNotSupportedException(supplier); // or findFirst().orElseThrow...
}

Is there any Java 8 feature that will let me checking my Stream status without terminating it?是否有任何 Java 8 功能可以让我在不终止它的情况下检查我的 Stream 状态?

The code below is a bit ugly but work like you wish.下面的代码有点难看,但可以像你希望的那样工作。

First we need to count how many agency of customers match with and then try found the first one supplier match.首先我们需要计算有多少客户匹配的代理商,然后尝试找到第一个供应商匹配。 If there are no matches throw an exception, but here you will check if the cause is that no agency clients were found in order to throw the correct excaption.如果没有匹配项抛出异常,但在这里您将检查原因是否是没有找到代理客户端以抛出正确的 excaption。

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);
        });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM