作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在写代码的时候,我想到了一个问题,HashMap中的值部分(Integer)在下面的场景中是否能够自增?
Map<String, Integer> dictionary = new HashMap<String, Integer>();
dictionary.put("a",1);
dictionary.put("b",1);
最佳答案
您可以使用 Multiset来自Guava由 Google 开源的框架。
使用 Multiset 可以极大地简化您的生活。
Multiset<String> set = HashMultiset.create();
set.add("abc"):
set.add("acd");
set.add("abc");
// use set.count(Object) to get the counter of the object
int c = set.count("abc");
// or iterate through the set to get each object and its count
for (Multiset.Entry<String> entry : set.entrySet()){
String str = entry.getElement();
int count = entry.getCount();
}
与使用普通HashMap的传统方式相比:
Map<String, Integer> map = new HashMap<String, Integer>();
public void add(String str){
Integer oldValue = map.get(str);
if (oldValue == null){
map.put(str, 1);
} else{
map.put(str, oldValue + 1);
}
}
即使使用可变计数器作为HashMap的值,代码仍然很繁琐。
Map<String, AtomicInteger> map = new HashMap<String, AtomicInteger>();
public void add(String str){
AtomicInteger counter = map.get(str);
if (counter == null){
counter = new AtomicInteger();
map.put(str, counter);
}
counter.incrementAndGet();
}
关于java - HashMap值自增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15996164/
1、修改数据 复制代码代码如下: DataRow dr =
数据库操纵基本流程为: 1、连接数据库服务器 2、选择数据库 3、执行SQL语句 4、处理结果集 5、打印操作信息 其中用到的相关函数有 •resource m
CRUD是Create(创建)、Read(读取)、Update(更新)和Delete(删除)的缩写,它是普通应用程序的缩影。如果您掌握了某框架的CRUD编写,那么意味可以使用该框架创建普通应用程序了
项目结构: 添加页面: &
本文实例讲述了android操作sqlite数据库(增、删、改、查、分页等)及listview显示数据的方法。分享给大家供大家参考,具体如下: 由于刚接触android开发,故此想把学到的基础知识
我是一名优秀的程序员,十分优秀!