网站建设鼎网络,开发大型网站的流程,wordpress虚拟目录图片打不开,上海天华建筑设计有限公司官网findfirst过滤Java 8 Stream #xff0c;通常使用findFirst()或findAny()来获取在过滤器中幸存的元素。 但这可能并不能真正实现您的意思#xff0c;并且可能会出现一些细微的错误。 那么 从我们的Javadoc#xff08; 此处和此处 #xff09;可以看出#xff0c;这两种方… findfirst 过滤Java 8 Stream 通常使用findFirst()或findAny()来获取在过滤器中幸存的元素。 但这可能并不能真正实现您的意思并且可能会出现一些细微的错误。 那么 从我们的Javadoc 此处和此处 可以看出这两种方法都从流中返回任意元素-除非流具有遇到顺序 在这种情况下 findFirst()返回第一个元素。 简单。 一个简单的示例如下所示 public OptionalCustomer findCustomer(String customerId) {return customers.stream().filter(customer - customer.getId().equals(customerId)).findFirst();
} 当然这只是旧的for-each-loop的漂亮版本 public OptionalCustomer findCustomer(String customerId) {for (Customer customer : customers)if (customer.getId().equals(customerId))return Optional.of(customer);return Optional.empty();
} 但是这两种变体都包含相同的潜在错误它们是基于隐含的假设建立的即只有一个具有任何给定ID的客户。 现在这可能是一个非常合理的假设。 也许这是一个已知的不变式由系统的专用部分保护并由其他人员依赖。 在这种情况下这完全可以。 通常代码依赖于唯一的匹配元素但没有做任何断言。 但是在很多情况下我不是在野外看到的。 也许客户只是从外部来源加载的这些来源无法保证其ID的唯一性。 也许现有的错误允许两本书具有相同的ISBN。 也许搜索词允许出乎意料的许多意外匹配有人说过正则表达式吗。 通常代码的正确性取决于以下假设存在与条件匹配的唯一元素但它不执行或断言该元素。 更糟糕的是不当行为完全是由数据驱动的可能会在测试期间将其隐藏。 除非考虑到这种情况否则我们可能会完全忽略它直到它在生产中出现为止。 更糟糕的是它默默地失败了 如果只有一个这样的元素的假设被证明是错误的我们将不会直接注意到这一点。 取而代之的是系统会在观察到影响并查明原因之前微妙地运行一段时间。 因此当然 findFirst()和findAny()本质上没有错。 但是使用它们很容易导致建模域逻辑中的错误。 Steven Depolo在CC-BY 2.0下发布 快速失败 因此让我们解决这个问题 假设我们非常确定最多有一个匹配元素如果没有我们希望代码快速失败 。 通过循环我们必须管理一些难看的状态它看起来如下 public OptionalCustomer findOnlyCustomer(String customerId) {boolean foundCustomer false;Customer resultCustomer null;for (Customer customer : customers)if (customer.getId().equals(customerId))if (!foundCustomer) {foundCustomer true;resultCustomer customer;} else {throw new DuplicateCustomerException();}return foundCustomer? Optional.of(resultCustomer): Optional.empty();
} 现在流为我们提供了更好的方法。 我们可以使用经常被忽略的reduce 文档中对此说 执行减少有关此流的元件使用缔合累积功能并返回一个可选描述的缩小值如果有的话。 这等效于 流减少 boolean foundAny false;
T result null;
for (T element : this stream) {if (!foundAny) {foundAny true;result element;}elseresult accumulator.apply(result, element);
}
return foundAny ? Optional.of(result) : Optional.empty(); 但不限于顺序执行。 看起来不像上面的循环吗 疯狂的巧合... 因此我们需要的是一个累加器该累加器会在调用后立即抛出所需的异常 public OptionalCustomer findOnlyCustomerWithId_manualException(String customerId) {return customers.stream().filter(customer - customer.getId().equals(customerId)).reduce((element, otherElement) - {throw new DuplicateCustomerException();});
} 这看起来有些奇怪但它确实可以满足我们的要求。 为了使它更具可读性我们应该将其放入Stream实用工具类中并给它起一个漂亮的名字 public static T BinaryOperatorT toOnlyElement() {return toOnlyElementThrowing(IllegalArgumentException::new);
}public static T, E extends RuntimeException BinaryOperatorT
toOnlyElementThrowing(SupplierE exception) {return (element, otherElement) - {throw exception.get();};
} 现在我们可以这样称呼它 // if a generic exception is fine
public OptionalCustomer findOnlyCustomer(String customerId) {return customers.stream().filter(customer - customer.getId().equals(customerId)).reduce(toOnlyElement());
}// if we want a specific exception
public OptionalCustomer findOnlyCustomer(String customerId) {return customers.stream().filter(customer - customer.getId().equals(customerId)).reduce(toOnlyElementThrowing(DuplicateCustomerException::new));
} 目的显示代码如何 这将实现整个流。 应该注意的是与findFirst()和findAny() 这当然不是短路操作 它将实现整个流。 也就是说如果确实只有一个元素。 当然一旦遇到第二个元素处理就会停止。 反射 我们已经看到findFirst()和findAny()如何不足以表示流中最多剩余一个元素的假设。 如果我们要表达该假设并确保在违反该代码时快速失败则需要reduce(toOnlyElement()) 。 您可以在GitHub上找到代码并随意使用-它在公共领域。 首先感谢Boris Terzic使我意识到这种意图不匹配。 翻译自: https://www.javacodegeeks.com/2016/02/beware-findfirst-findany.htmlfindfirst