freelancerh
4/22/2020 - 6:53 AM

map中value为int时,实现value自增

//方法1。使用jdk1.8 map的新方法merge
map.merge(key, 1, Integer::sum) 
//方法2。先get后set
int count = map.containsKey(word) ? map.get(word) : 0;
map.put(word, count + 1);
//方法3。当value为AtomiclLog
map.putIfAbsent(word, new AtomicLong(0));
map.get(word).incrementAndGet()