首页 > 解决方案 > 如果值不为空或不为空,如何使用 java 8 流验证列表中的对象值

问题描述

我有一个对象列表

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

还尝试了一个过滤器中的所有条件

标签: javalistiteratorjava-stream

解决方案


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

然后,您所需要的只是遍历 offer ( Stream<OfferToCheck>) 并为每一个遍历表示提取和验证本身的映射。

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

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

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)。
  • 需要适当的单元测试来涵盖这样的逻辑(无论如何我都会推荐你,这个实现意味着对测试的强烈需求)。

推荐阅读