You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

移除Map中满足特定条件的条目问题排查

Why Your Map Entries Aren't Being Removed

Your code has two critical issues preventing entries from being removed as expected:

1. You’re Trying to Remove by Value, Not Key

The Map.remove(Object key) method removes entries using their key, not their value. When you call under20.remove(item), you’re passing an ExchangeSummaryItem object as the key—but your map’s keys are Strings. Since no string key matches the ExchangeSummaryItem instance, this call does nothing at all.

2. Concurrent Modification Risk (Even If You Had the Key)

Iterating directly over under20.values() and modifying the map (like removing entries) can trigger a ConcurrentModificationException in standard Map implementations (e.g., HashMap). This is because the iterator is fail-fast and detects structural changes during iteration.


Fixes to Remove Entries Correctly

Here are three reliable approaches to solve this problem:

Option 1: Iterate Over Entry Set with an Iterator

Use an iterator on the map’s entry set to safely check and remove entries without risking concurrent modification errors:

Iterator<Map.Entry<String, ExchangeSummaryItem>> iterator = under20.entrySet().iterator();
while (iterator.hasNext()) {
    Map.Entry<String, ExchangeSummaryItem> entry = iterator.next();
    ExchangeSummaryItem item = entry.getValue();
    
    int sellAverage = item.getSellAverage();
    int sellQuantity = item.getSellQuantity();
    int buyQuantity = item.getBuyQuantity();
    
    if (sellAverage > 20000 && sellQuantity == 0 && buyQuantity == 0) {
        // Note: Verify if 20000 is intentional (your problem statement mentions 2000)
        iterator.remove(); // Safely removes the current entry
    }
}

Option 2: Use Java 8+ Streams to Filter the Map

Create a new map containing only the entries you want to keep—this is a clean, functional approach that avoids iteration issues entirely:

Map<String, ExchangeSummaryItem> filteredUnder20 = under20.entrySet().stream()
    .filter(entry -> {
        ExchangeSummaryItem item = entry.getValue();
        int sellAverage = item.getSellAverage();
        int sellQuantity = item.getSellQuantity();
        int buyQuantity = item.getBuyQuantity();
        
        // Keep entries that do NOT meet the removal condition
        return !(sellAverage > 20000 && sellQuantity == 0 && buyQuantity == 0);
    })
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

// Replace the original map if needed
under20 = filteredUnder20;

Option 3: Iterate Over a Copy of the Key Set

If you prefer a more traditional loop, iterate over a copy of the map’s key set to safely remove entries from the original map:

// Create a copy of the key set to avoid ConcurrentModificationException
for (String key : new ArrayList<>(under20.keySet())) {
    ExchangeSummaryItem item = under20.get(key);
    
    int sellAverage = item.getSellAverage();
    int sellQuantity = item.getSellQuantity();
    int buyQuantity = item.getBuyQuantity();
    
    if (sellAverage > 20000 && sellQuantity == 0 && buyQuantity == 0) {
        under20.remove(key); // Now using the correct key to remove the entry
    }
}

Quick Check

Your code checks for sellAverage > 20000, but your problem statement mentions sell_average > 2000. Double-check that this threshold is intentional—if it’s a typo, adjust the number accordingly!

内容的提问来源于stack exchange,提问作者Jacob Windolf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:47:41