第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

Java通用方法來驗(yàn)證對象參數(shù)中的空值

Java通用方法來驗(yàn)證對象參數(shù)中的空值

慕慕森 2021-07-22 14:02:23
我正在嘗試實(shí)現(xiàn)一個(gè)邏輯,其中我有一個(gè)具有 7 個(gè)屬性的 POJO 類。我已將這些 POJO 類添加到地圖中,具體取決于屬性的值。下面是實(shí)現(xiàn)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   }}我的問題是,如果循環(huán)是否有任何泛化實(shí)現(xiàn),我可以在這里嘗試,而不是寫這么多。
查看完整描述

3 回答

?
拉莫斯之舞

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個(gè)贊

一個(gè)可能的最佳解決方案將類似于一個(gè)我已經(jīng)在今天早些時(shí)候建議。


使用 將已檢查屬性Map<String, Optional<?>>的Optional值與未來輸出映射鍵的鍵一起存儲。


Map<String, Optional<?>> options = new HashMap<>();

options.put("attribute1", Optional.ofNullable(price.getAttribute1()));

// ...

options.put("attribute3", Optional.ofNullable(price.getAttribute2()));

// ...

使用索引的迭代可以讓您執(zhí)行地圖的更新。


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

for (int i=1; i<7; i++) {                                      // attributes 1..7

    String attribute = "attribute" + i;                        // attribute1...attribute7

    options.get(attribute).ifPresent(any ->                    // for non-nulls

               map.put(                                        // put to the map

                   attribute,                                  // attribute as key remains

                   Optional.ofNullable(map.get(attribute))     // gets the existing list

                           .orElse(new ArrayList<>())          // or creates empty

                           .add(price)));                      // adds the current Price

}

此外,我敢打賭你的意圖有點(diǎn)不同。沒有方法Map::set


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

你不是想把List<Price>一個(gè)項(xiàng)目放在同一個(gè)鍵上嗎?


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

因此,您可以使用我上面發(fā)布的方式。


查看完整回答
反對 回復(fù) 2021-07-29
?
一只萌萌小番薯

TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊

您可以使用


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

for(PriceClass price: prices) {

    HashMap<String,Object> options = new HashMap<>();

    options.put("attibute1", price.getAttribute1());

    options.put("attibute2", price.getAttribute2());

    options.put("attibute3", price.getAttribute3());

    options.put("attibute4", price.getAttribute4());

    options.put("attibute5", price.getAttribute5());

    options.put("attibute6", price.getAttribute6());

    options.put("attibute7", price.getAttribute7());

    options.values().removeIf(Objects::isNull);

    options.keySet().forEach(attr -> map.computeIfAbsent(attr, x -> new ArrayList<>())

                                        .add(price));

}

或概括過程:


一次準(zhǔn)備一個(gè)不可修改的地圖


static final Map<String, Function<PriceClass,Object>> ATTR;

static {

  Map<String, Function<PriceClass,Object>> a = new HashMap<>();

  a.put("attibute1", PriceClass::getAttribute1);

  a.put("attibute2", PriceClass::getAttribute2);

  a.put("attibute3", PriceClass::getAttribute3);

  a.put("attibute4", PriceClass::getAttribute4);

  a.put("attibute5", PriceClass::getAttribute5);

  a.put("attibute6", PriceClass::getAttribute6);

  a.put("attibute7", PriceClass::getAttribute7);

  ATTR = Collections.unmodifiableMap(a);

}

并使用


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

for(PriceClass price: prices) {

    HashMap<String,Object> options = new HashMap<>();

    ATTR.forEach((attr,func) -> options.put(attr, func.apply(price)));

    options.values().removeIf(Objects::isNull);

    options.keySet().forEach(attr -> map.computeIfAbsent(attr, x -> new ArrayList<>())

                                        .add(price));

}

要么


Map<String,List<PriceClass>> map = prices.stream()

    .flatMap(price -> ATTR.entrySet().stream()

        .filter(e -> e.getValue().apply(price) != null)

        .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), price)))

    .collect(Collectors.groupingBy(Map.Entry::getKey,

                Collectors.mapping(Map.Entry::getValue, Collectors.toList())));


查看完整回答
反對 回復(fù) 2021-07-29
?
狐的傳說

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊

如何使用 egEnum定義 7 個(gè)不同的對象,每個(gè)對象負(fù)責(zé)具體屬性:


// this is client code, looks pretty easy

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


for (PriceClass price : prices)

    PriceAttribute.add(map, price);



// all logic is hidden within special Enum    

enum PriceAttribute {

    ATTRIBUTE1("attribute1", PriceClass::getAttribute1),

    ATTRIBUTE2("attribute2", PriceClass::getAttribute2),

    ATTRIBUTE3("attribute3", PriceClass::getAttribute3),

    ATTRIBUTE4("attribute4", PriceClass::getAttribute4),

    ATTRIBUTE5("attribute5", PriceClass::getAttribute5),

    ATTRIBUTE6("attribute6", PriceClass::getAttribute6),

    ATTRIBUTE7("attribute7", PriceClass::getAttribute7);


    private final String key;

    private final Function<PriceClass, ?> get;


    PriceAttribute(String key, Function<PriceClass, ?> get) {

        this.key = key;

        this.get = get;

    }


    public static void add(Map<String, List<PriceClass>> map, PriceClass price) {

        for (PriceAttribute attribute : values()) {

            if (attribute.get.apply(price) != null) {

                map.computeIfAbsent(attribute.key, key -> new ArrayList<>()).add(price);    

                break;

            }

        }

    }


查看完整回答
反對 回復(fù) 2021-07-29
  • 3 回答
  • 0 關(guān)注
  • 300 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號