Counting occurrences of a value on a map
I have a card of the following type:
private HashMap<Integer, HashMap<String, Object>> entireMap;
Keys run from 1 - n. The subset within the whole map is of the following type:
HashMap<String, Object> subMap = new HashMap<String, Object>();
Each key of the entire map contains this subset (and more):
subMap.put("user_name", usid[1]);
So, I have something like this at the end:
{1 {"user_name" = "Arthur", "otherKeys = ..."}}
{2 {"user_name" = "Bela", "otherKeys = ..."}}
{3 {"user_name" = "Ceasar", "otherKeys = ..."}}
{4 {"user_name" = "Ceasar", "otherKeys = ..."}}
{5 {"user_name" = "Bela", "otherKeys = ..."}}
{6 {"user_name" = "Bela", "otherKeys = ..."}}
Now I want to count the maximum number of username in the whole array, in this case it will be 3 (bela happens three times).
How can i do this?
source to share
Here's an example implementation.
Note : do not use this kind of card initialization in production code!
HashMap<Integer, HashMap<String, Object>> entireMap = new HashMap<>();
entireMap.put(1, new HashMap<String, Object>() {{
put("user_name", "Arthur");
put("other_key1", "val");
put("other_key2", "val");
}});
entireMap.put(2, new HashMap<String, Object>() {{
put("user_name", "Bela");
put("other_key2", "val");
}});
entireMap.put(3, new HashMap<String, Object>() {{
put("user_name", "Ceasar");
}});
entireMap.put(4, new HashMap<String, Object>() {{
put("user_name", "Ceasar");
}});
entireMap.put(5, new HashMap<String, Object>() {{
put("user_name", "Bela");
}});
entireMap.put(6, new HashMap<String, Object>() {{
put("user_name", "Bela");
}});
Map<Object, Long> result = entireMap
.values()
.stream()
.map(map -> map.get("user_name"))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(result);
Long max = Collections.max(result.values());
System.out.println(max);
Output:
{Ceasar=2, Arthur=1, Bela=3} 3
source to share
If you don't want to use a java 8 feature, you can do it by simply iterating over the map.
Map<String, Integer> resultMap = new HashMap<String, Integer>();
for (int key : entireMap.keySet()) {
String userName = (String) entireMap.get(key).get("user_name");
if (resultMap.containsKey(userName)) {
resultMap.put(userName, resultMap.get(userName) + 1);
} else {
resultMap.put(userName, 1);
}
}
System.out.println(resultMap);
Output:
{Arthur = 1, Ceasar = 2, Bela = 3}
source to share
Here's one way to do it using Java 8 features, but no streams:
Map<String, Long> counts = new HashMap<>();
entireMap.forEach((k, v) ->
counts.merge(v.get("user_name"), 1L, Long::sum));
On the card counts
, you will have an account for each user. Then you can find the entry with the maximum value:
Entry<String, Long> max = Collections.max(
counts.entrySet(),
Map.Entry.comparingByValue());
source to share
In Java 7 or below, you can use Guava MultiSet :
List<Object> names = new ArrayList<>();
for (HashMap<String, Object> outerMap : entireMap.values()) {
names.add(outerMap.get("user_name"));
}
HashMultiset<Object> objects = HashMultiset.create(names);
objects.forEachEntry((name, count) -> System.out.println(name + " -> " + count));
As a result:
Ceasar -> 2
Arthur -> 1
Bela -> 3
source to share