我有一个元素流,只想从流中获取一个元素,如果流中有多个元素并且根本没有元素,则抛出异常.可以用Stream制作吗?
public Entity getEntityByValue(String value) {
return entityService.getEntitiesByValue(value)
.stream
// check here count of entities and throw a MultiMatchException if more than one entity is present
.findAny()
.orElseThrow(() -> throw NoEntitiesFoundException()); //no entities found
}
我想过使用.duce(a,b->;抛出多匹配异常),并在第二个元素存在但不能将其与其他代码组合时抛出异常