【问题标题】:How to validate object values in a List if value is not empty or not null using java 8 stream如果值不为空或不为空,如何使用 java 8 流验证列表中的对象值
【发布时间】:2021-01-07 07:31:17
【问题描述】:

我有一个对象列表

    OfferToCheck offer = new OfferToCheck();
    
    offer.setDiscountId(null);
    offer.setProductName("sadf spacefjkd");
    offer.setServiceCode("dfsdfv");
    offer.setServiceGroup("nospace");

    OfferToCheck offer1 = new OfferToCheck();
    offer1.setDiscountId("");
    offer1.setProductName("sadfspacefjkd");
    offer1.setServiceCode("sdnof");
    offer1.setServiceGroup("sdf");
    
    request.setOffersToCheck(Arrays.asList(offer,offer1));      
    request.setStartTrialPeriod("y");
if(service.validateStatus(request)){
        System.out.println("true");
    }
    else{
        System.out.println("false");
    }

我想使用 java 8 流实现这个 for 循环

for(OfferToCheck offer : request.getOffersToCheck()){
        if(offer.getDiscountId() != null && !offer.getProductName().isEmpty()){
            if(!offer.getDiscountId().chars().allMatch(Character::isDigit)){
                return isValid = false;
            }
            
        }
        if(offer.getServiceCode() != null && !offer.getServiceCode().isEmpty()){
            if(!offer.getServiceCode().chars().allMatch(Character::isAlphabetic)){
                return isValid = false;
            }
        }
        if(offer.getServiceGroup() != null && !offer.getServiceGroup().isEmpty()){
            if(!offer.getServiceGroup().chars().allMatch(Character::isAlphabetic)){
                return isValid = false;
            }
        }
        if(offer.getProductName() != null && !offer.getProductName().isEmpty()){
            if(!offer.getProductName().matches("[a-zA-Z ]*")){
                return isValid = false;
            }
        }
    }

我已经尝试过了,但是当 discountId 的值为空或 null 时,它不会将对象的其余部分包含到流中。

    if(!request.getOffersToCheck().stream()
              .filter(list -> (list.getServiceCode() != null && !list.getServiceCode().isEmpty()))
              .filter  (list -> list.getDiscountId() != null && !list.getDiscountId().isEmpty()) 
              .filter  (list -> list.getProductName() != null && !list.getProductName().isEmpty())
              .filter (list -> list.getProductName() != null && !list.getProductName().isEmpty()) 
              .filter (list -> list.getServiceGroup() != null && !list.getServiceGroup().isEmpty())
              .allMatch(validateList -> (validateList .getDiscountId().chars().allMatch(Character::isLetterOrDigit)
                      && validateList.getProductName().matches("[a-zA-Z0-9 ]*")
                      && validateList.getServiceCode().matches("[a-zA-Z0-9]*")
                      && validateList.getServiceGroup().chars().allMatch(Character::isDigit))
                      ))
    {
        return isValid = false;
    }

也在一个过滤器中尝试了所有条件

【问题讨论】:

  • 在写我的答案时,我觉得你有一个错字:if(offer.getDiscountId() != null && !offer.getProductName().isEmpty()){ 在第二个 sn-p 中应该是 if(offer.getDiscountId() != null && !offer.getDiscountId().isEmpty()){

标签: java list iterator java-stream


【解决方案1】:

您可以创建字段映射的映射(使用Function)作为键,Predicate 作为字段验证本身的值。

然后,您只需遍历报价 (Stream<OfferToCheck>) 并为每个报价遍历代表提取和验证本身的地图。

  • 使用它的键提取字段值(即String fieldValue = entry.getKey().apply(offer)
  • 使用它的值来验证字段值(即entry.getValue().test(fieldValue)

您想从Stream::allMatch 收集所有boolean 结果并确保它们都是正确的,从而返回该结果本身。

Map<Function<OfferToCheck, String>, Predicate<String>> map = new HashMap<>();
map.put(OfferToCheck::getDiscountId, s -> s.chars().allMatch(Character::isDigit));
map.put(OfferToCheck::getServiceCode, s -> s.chars().allMatch(Character::isAlphabetic));
map.put(OfferToCheck::getServiceGroup, s -> s.chars().allMatch(Character::isAlphabetic));
map.put(OfferToCheck::getProductName, s -> s.matches("[a-zA-Z ]*"));

return request.getOffersToCheck()
     .stream()
     .allMatch(offer -> map.entrySet()
              .stream()
              .filter(entry -> entry.getKey().apply(offer) != null && 
                              !entry.getKey().apply(offer).isEmpty())
              .allMatch(entry -> entry.getValue().test(entry.getKey().apply(offer))));

此解决方案假定所有字段都以类似方式验证 - 例如。使用String::isEmpty。否则,您必须将此类验证放入地图并将其从内部流中删除。随意根据需要修改代码。

您是否接受此解决方案的最终选择完全取决于您:

优点

  • 对于大量类似的字段验证很有用,例如。肥皂。
  • 可扩展:您为每个新字段添加的唯一内容就是一个新的 map 条目。

缺点

  • 更难阅读、维护和调试(您必须使用Stream::peek)。
  • 需要适当的单元测试来涵盖此类逻辑(无论如何我都建议您,这种实现意味着对测试的强烈需求)。

【讨论】:

  • 感谢您的回答。这意味着使用流并不总是一个好主意。
  • 没错!我认为您可以根据我所描述的利弊做出决定 - 它适用于这种特殊情况。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
  • 2020-06-11
  • 2019-03-04
  • 1970-01-01
相关资源
最近更新 更多