【发布时间】:2016-04-03 16:05:52
【问题描述】:
如果键的值为 zero(0),我想从地图中删除键,我可以使用 map.values().removeAll(Collections.singleton(0l));
来实现它。
在我使用 Map<String,Long> 之前它运行良好,但现在我们已将实现更改为 Map<String,AtomicLong> 现在它不会删除值为零,因为我使用原子变量作为值。
我试过的小代码sn-p ::
Map<String, AtomicLong> atomicMap = new HashMap<String,AtomicLong>();
atomicMap.put("Ron", new AtomicLong(0l));
atomicMap.put("David", new AtomicLong(0l));
atomicMap.put("Fredrick", new AtomicLong(0l));
atomicMap.put("Gema", new AtomicLong(1l));
atomicMap.put("Andrew", new AtomicLong(1l));
atomicMap.values().removeAll(Collections.singleton(new AtomicLong(0l)));
System.out.println(atomicMap.toString());
输出为 {Ron=0, Fredrick=0, Gema=1, Andrew=1, David=0}
如您所见,值为 0 的键并未被删除。任何人都可以提出解决方案,这将有很大帮助。
谢谢。
【问题讨论】:
标签: java dictionary concurrency atomic key-value