Java:如何使用一对HashMap的键 [英] Java: How to use a pair of keys for HashMap

查看:183
本文介绍了Java:如何使用一对HashMap的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从sql数据库中,我正在读取一个包含2个字段的表:appName,user。所以我可以看到:

From a sql database, I am reading a table with 2 fields: appName, user. So I may see:

+-------------+
| appA | John |
+-------------+
| appB | Mary |
+-------------+
| appC | Tom  |
+-------------+
| appA | John |
+-------------+
| appA | Mary |
+-------------+

这些记录作为AppClass的对象存储到appName和user中。
现在,我想列出不同用户运行的应用程序的次数。所以:

Each of these records is stored as an object of a AppClass with appName and user. Now, i want to list how many times the apps were run by different users. So :

+-----------------+
| appA | John | 2 |
+-----------------+
| appA | Mary | 1 | 
+-----------------+
| appB | Mary | 1 |  
+-----------------+
| appC | Tom  | 1 | 
+-----------------+

是否可以使用带有2个键的HashMap进行计数?

Is it possible do the counting with a HashMap with 2 keys?

推荐答案

是的,您可以使用以下结构:

Yes, you can have a structure like the following:

Map<String, Map<String, Integer>>
       ^           ^       ^
       appName    user    count

但这会更容易如果您直接从数据库获取汇总数据:

But it'd be much easier if you just fetch the aggregated data directly from the database:

SELECT appName, user, COUNT(*)
FROM table
GROUP BY appName, user;

这篇关于Java:如何使用一对HashMap的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆