猿问

Java通用方法来验证对象参数中的空值

我正在尝试实现一个逻辑,其中我有一个具有 7 个属性的 POJO 类。我已将这些 POJO 类添加到地图中,具体取决于属性的值。


下面是实现


Map<String,List<PriceClass>> map = new HashMap();

for (PriceClass price : prices) {

  if (price.getAttribute1() !=null) {

      if (map.get("attribute1") !=null) {

             map.get("attribute1").add(price);

      } else {

           map.set("attibute1",Collections.singletonList(price))

      }

   } else if(price.getAttribute2()!=null) {

       if (map.get("attribute12") !=null) {

             map.get("attribute2").add(price);

       } else {

           map.set("attibute2",Collections.singletonList(price))

       }

   } else if (price.getAttribute3() !=null) {

     .

     .

     .

   } else if (price.getAttribute7() !=null) {

       //update the map

   }

}

我的问题是,如果循环是否有任何泛化实现,我可以在这里尝试,而不是写这么多。


慕慕森
浏览 266回答 3
3回答

拉莫斯之舞

一个可能的最佳解决方案将类似于一个我已经在今天早些时候建议。使用 将已检查属性Map<String, Optional<?>>的Optional值与未来输出映射键的键一起存储。Map<String, Optional<?>> options = new HashMap<>();options.put("attribute1", Optional.ofNullable(price.getAttribute1()));// ...options.put("attribute3", Optional.ofNullable(price.getAttribute2()));// ...使用索引的迭代可以让您执行地图的更新。Map<String,List<Price>> map = new HashMap();for (int i=1; i<7; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // attributes 1..7&nbsp; &nbsp; String attribute = "attribute" + i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // attribute1...attribute7&nbsp; &nbsp; options.get(attribute).ifPresent(any ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // for non-nulls&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;map.put(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // put to the map&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;attribute,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // attribute as key remains&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Optional.ofNullable(map.get(attribute))&nbsp; &nbsp; &nbsp;// gets the existing list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.orElse(new ArrayList<>())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // or creates empty&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.add(price)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // adds the current Price}此外,我敢打赌你的意图有点不同。没有方法Map::setmap.set("attibute1",Collections.singletonList(price))你不是想把List<Price>一个项目放在同一个键上吗?map.put("attibute1", Collections.singletonList(price))因此,您可以使用我上面发布的方式。

一只萌萌小番薯

您可以使用Map<String,List<PriceClass>> map = new HashMap<>();for(PriceClass price: prices) {&nbsp; &nbsp; HashMap<String,Object> options = new HashMap<>();&nbsp; &nbsp; options.put("attibute1", price.getAttribute1());&nbsp; &nbsp; options.put("attibute2", price.getAttribute2());&nbsp; &nbsp; options.put("attibute3", price.getAttribute3());&nbsp; &nbsp; options.put("attibute4", price.getAttribute4());&nbsp; &nbsp; options.put("attibute5", price.getAttribute5());&nbsp; &nbsp; options.put("attibute6", price.getAttribute6());&nbsp; &nbsp; options.put("attibute7", price.getAttribute7());&nbsp; &nbsp; options.values().removeIf(Objects::isNull);&nbsp; &nbsp; options.keySet().forEach(attr -> map.computeIfAbsent(attr, x -> new ArrayList<>())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add(price));}或概括过程:一次准备一个不可修改的地图static final Map<String, Function<PriceClass,Object>> ATTR;static {&nbsp; Map<String, Function<PriceClass,Object>> a = new HashMap<>();&nbsp; a.put("attibute1", PriceClass::getAttribute1);&nbsp; a.put("attibute2", PriceClass::getAttribute2);&nbsp; a.put("attibute3", PriceClass::getAttribute3);&nbsp; a.put("attibute4", PriceClass::getAttribute4);&nbsp; a.put("attibute5", PriceClass::getAttribute5);&nbsp; a.put("attibute6", PriceClass::getAttribute6);&nbsp; a.put("attibute7", PriceClass::getAttribute7);&nbsp; ATTR = Collections.unmodifiableMap(a);}并使用Map<String,List<PriceClass>> map = new HashMap<>();for(PriceClass price: prices) {&nbsp; &nbsp; HashMap<String,Object> options = new HashMap<>();&nbsp; &nbsp; ATTR.forEach((attr,func) -> options.put(attr, func.apply(price)));&nbsp; &nbsp; options.values().removeIf(Objects::isNull);&nbsp; &nbsp; options.keySet().forEach(attr -> map.computeIfAbsent(attr, x -> new ArrayList<>())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add(price));}要么Map<String,List<PriceClass>> map = prices.stream()&nbsp; &nbsp; .flatMap(price -> ATTR.entrySet().stream()&nbsp; &nbsp; &nbsp; &nbsp; .filter(e -> e.getValue().apply(price) != null)&nbsp; &nbsp; &nbsp; &nbsp; .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), price)))&nbsp; &nbsp; .collect(Collectors.groupingBy(Map.Entry::getKey,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.mapping(Map.Entry::getValue, Collectors.toList())));

狐的传说

如何使用 egEnum定义 7 个不同的对象,每个对象负责具体属性:// this is client code, looks pretty easyMap<String, List<PriceClass>> map = new HashMap<>();for (PriceClass price : prices)&nbsp; &nbsp; PriceAttribute.add(map, price);// all logic is hidden within special Enum&nbsp; &nbsp;&nbsp;enum PriceAttribute {&nbsp; &nbsp; ATTRIBUTE1("attribute1", PriceClass::getAttribute1),&nbsp; &nbsp; ATTRIBUTE2("attribute2", PriceClass::getAttribute2),&nbsp; &nbsp; ATTRIBUTE3("attribute3", PriceClass::getAttribute3),&nbsp; &nbsp; ATTRIBUTE4("attribute4", PriceClass::getAttribute4),&nbsp; &nbsp; ATTRIBUTE5("attribute5", PriceClass::getAttribute5),&nbsp; &nbsp; ATTRIBUTE6("attribute6", PriceClass::getAttribute6),&nbsp; &nbsp; ATTRIBUTE7("attribute7", PriceClass::getAttribute7);&nbsp; &nbsp; private final String key;&nbsp; &nbsp; private final Function<PriceClass, ?> get;&nbsp; &nbsp; PriceAttribute(String key, Function<PriceClass, ?> get) {&nbsp; &nbsp; &nbsp; &nbsp; this.key = key;&nbsp; &nbsp; &nbsp; &nbsp; this.get = get;&nbsp; &nbsp; }&nbsp; &nbsp; public static void add(Map<String, List<PriceClass>> map, PriceClass price) {&nbsp; &nbsp; &nbsp; &nbsp; for (PriceAttribute attribute : values()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (attribute.get.apply(price) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.computeIfAbsent(attribute.key, key -> new ArrayList<>()).add(price);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}&nbsp;
随时随地看视频慕课网APP

相关分类

Java
我要回答