如何在Java中执行(sql)组(How to perform a group by (sql) in Java)
我有一个文本文件,如下所示:
code appearance ---------------- j4t8 1 fj89 3 pf6n 1 j4t8 5我想按照看起来最多的代码进行排序。 正如您所看到的(并且由于我想执行分组),存在重复的代码,因此使用HashMap会出现问题(重复键)。 有任何想法吗?
I have a text file which looks like this:
code appearance ---------------- j4t8 1 fj89 3 pf6n 1 j4t8 5And I want to sort by the codes which appear the most. As you can see (and since I want to perform a group by) there are duplicate codes, so using HashMap would be a problem (duplicate keys). Any ideas?
最满意答案
不知道这是否是最好的解决方案,但你可以创建一个列表的地图,如下所示:
Map<String, List<Integer>> map = new HahsMap<String, List<Integer>>(); if(map.contains.(key)) { map.get(key).add(new_appearance_value); } else { List<Integer> app = new ArrayList<Integer>(); app.add(new_appearance_value); map.put(key, app); }地图键将是代码,外观的值将进入列表。
注意:要确定哪些代码具有更多外观,只需检查每个代码列表的大小。
don't know if this is the best solution but you could create a map of a list like this:
Map<String, List<Integer>> map = new HahsMap<String, List<Integer>>(); if(map.contains.(key)) { map.get(key).add(new_appearance_value); } else { List<Integer> app = new ArrayList<Integer>(); app.add(new_appearance_value); map.put(key, app); }Where the map key would be the code and the values of appearance would go into the list.
Note: to determine which code has more appearances just check for the size of the list of each code.
更多推荐
发布评论