计算元素出现次数并存入map,急!

chaser401 2016-05-10 05:17:20
我现在想要遍历数据库并记录id出现次数并存入map中,请问如何实现,我的方法:
while (rs.next()) {
radio = 0;
if (rs.getInt(''app_id'') != null) {
app_sum.put(rs.getInt("app_id"), app_sum.get(rs.getInt("app_id")) + radio);
count++;
app_day.put(rs.getInt("app_id"), count);
} else {
app_sum.put(rs.getInt("app_id"), radio);
app_day.put(rs.getInt("app_id"), 1);
count = 1;
}
}
如果结果无序的话会出问题,请问如何改进?
...全文
203 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangxheng 2016-05-11
  • 打赏
  • 举报
回复
亲,你是不是这个意思
class Entity{
		int radio;
		int app_id;
		//还可以有其它信息
	}
你是信息过去,map的值放入不够用是吧,然后把你们map的value设为你自定义的对像
HashMap<Integer, Entity> entityList = new HashMap<Integer, Entity>();
		
		while (rs.next()) {
			int radio = 0;
			if (rs.getInt("app_id") != null) {
					app_sum.put(rs.getInt("app_id"), app_sum.get(rs.getInt("app_id")) + radio);
					count++;
					app_day.put(rs.getInt("app_id"), count);
					
					entityList.put();//把东西放之里面
				} else {
					app_sum.put(rs.getInt("app_id"), radio);
					app_day.put(rs.getInt("app_id"), 1);
					count = 1;
				}
		}
  • 打赏
  • 举报
回复
首先,id不应该是唯一的吗?好吧,这不是重点。统计不可以用sql语句统计吗?好吧这也不是重点。 你用map存,前边放id,后边放数量,后边用个三木运算符,第一次数量置1已经存在了就++
Aki009 2016-05-11
  • 打赏
  • 举报
回复
LinkedHashMap
蹭蹭一笑 2016-05-11
  • 打赏
  • 举报
回复
如果你是想统计数据库中所有id,出现的次数是多少,代码并不要这样写的
jsf06 2016-05-11
  • 打赏
  • 举报
回复
不是很清楚你的需求

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧