亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如果對象滿足特定條件,則從 Map 中刪除條目

如果對象滿足特定條件,則從 Map 中刪除條目

慕田峪4524236 2022-09-14 10:19:42
我有一個對象映射,如果對象屬性滿足特定條件,我想從映射中刪除它。地圖如下Map<String, ExchangeSummaryItem> under20 = mapper.readValue(new URL("https://rsbuddy.com/exchange/summary.json"), new TypeReference<Map<String, ExchangeSummaryItem>>() {});每個 ExchangeSummary 都有一個sell_average、sell_quantity和buy_quantity,如果sell_average > 2000,并且買入/賣出數量均為 0,我想將其從地圖中刪除。我當前的代碼如下所示,但無法成功從映射中刪除任何值(映射仍然具有相同的大?。ゝor (ExchangeSummaryItem item : under20.values()) {     int ObjSellAverage = item.getSellAverage();     int ObjSellQ = item.getSellQuantity();     int ObjBuyQ = item.getBuyQuantity();     if (ObjSellAverage > 20000 && ObjSellQ == 0 && ObjBuyQ == 0){          System.out.println(under20.size());          under20.remove(item);     }}任何關于為什么會發生這種情況的幫助將不勝感激!謝謝!
查看完整描述

1 回答

?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

under20.remove(item);是使用值進行調用。它期待鑰匙。你也不能只是改為迭代和調用,因為你會有一個.removeunder20.keySet()removeConcurrentModificationException


解決它的一種簡單方法是創建另一個地圖:


Map<String, ExchangeSummaryItem> result = new HashMap<>();


//Map.entrySet() gives you access to both key and value.

for (Map.Entry<String,ExchangeSummaryItem> item : under20.entrySet()) {

     int ObjSellAverage = item.getValue().getSellAverage();

     int ObjSellQ = item.getValue().getSellQuantity();

     int ObjBuyQ = item.getValue().getBuyQuantity();


     if (!(ObjSellAverage > 20000 && ObjSellQ == 0 && ObjBuyQ == 0)){

          result.put(item.getKey(), item.getValue());

     }

}

并在result


查看完整回答
反對 回復 2022-09-14
  • 1 回答
  • 0 關注
  • 106 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號