`
rigger21
  • 浏览: 126417 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

HashMap实现同键值累加

阅读更多
<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>

如何用hashmap对一个集合实现相同的键,值累加,而非替换?

解决方法如下:

HashMap map =new HashMap();

if(循环取出aa bb的值)

{

String aa="获得aa的值";

String bb="获得bb的值";

if(map.containsKey(aa))//判断集合里是否有aa键

{

map.put(aa,map.get(aa)+bb);//map.get()方法取出对应键的值

}

else//否则在map中添加新键值

{

map.put(aa,bb);

}

}

//遍历map

Set entries = map.entrySet();
Iterator iter = entries.iterator();
while(iter.hasNext())
{
Map.Entry entry = (Map.Entry)iter.next();
String key = entry.getKey();//得到键
String value = entry.getValue();//得到值
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics