java123999
java123999

Reputation: 7394

Finding number of values in a HashMap?

What is the best / most efficient way to find the total number of Values in a HashMap.

I do not mean the .size() method as it counts the number of keys. I want the total number of values in all the keys.

I want to do so as my key is a String, but my value is a List.

Upvotes: 13

Views: 20644

Answers (7)

Clashsoft
Clashsoft

Reputation: 11882

In Java 8, you can also utilize the Stream API:

int total = map.values()
               .stream()
               .mapToInt(List::size) // or (l -> l.size())
               .sum()

This has the advantage that you don't have to repeat the List<Foo> type for a for variable, as in the pre-Java 8 solution:

int total = 0;
for (List<Foo> list : map.values())
{
    total += list.size();
}
System.out.println(total);

In addition to that, although not advised, you could also use that value inline without needing a temp variable:

System.out.println(map.values().stream().mapToInt(List::size).sum());

Upvotes: 16

Mostafa Hussien
Mostafa Hussien

Reputation: 11

import java.util.HashMap;
public class Solution {  
    public static void main(String args[]) {
        int total = 0;
        HashMap<String,String> a = new HashMap<String,String>();
        a.put("1.","1");
        a.put("2.","11");
        a.put("3.","21");
        a.put("4.","1211");
        a.put("5.","111221");          
        for (String l : a.values()) {
            total ++;
        }
        System.out.println(total);       
    }
}

Upvotes: 1

itohro
itohro

Reputation: 161

With Eclipse Collections, the following will work using MutableMap.

MutableMap<String, List<String>> map = 
        Maps.mutable.of("key1", Lists.mutable.of("a", "b", "c"),
                        "key2", Lists.mutable.of("d", "e", "f", "g"));

long total = map.sumOfInt(List::size);

Note: I am a committer for Eclipse Collections.

Upvotes: 4

Tunaki
Tunaki

Reputation: 137064

Starting with Java 8, given a Map<K, List<V>> map you could use the Stream API and have:

int size = map.values().stream().mapToInt(List::size).sum();

This creates a Stream of the values with stream(), maps each of them to their size with mapToInt, where the mapper is the method reference List::size refering to List#size(), and sum the results with sum().

Upvotes: 4

Mureinik
Mureinik

Reputation: 310993

If I understand the question correctly, you have a Map<String, List<Something>>, and you want to count the total number of items in all the Lists in the Map's values. Java 8 offers a pretty easy way of doing this by streaming the values, mapping them to their size() and then just summing them:

Map<String, List<Something>> map = ...;
int totalSize = map.values().stream().mapToInt(List::size).sum());

Upvotes: 3

Suresh Atta
Suresh Atta

Reputation: 121998

Easiest would be, iterate and add over list sizes.

int total = 0;
for (List<Foo> l : map.values()) {
    total += l.size();
}

// here is the total values size

Upvotes: 10

bobasti
bobasti

Reputation: 1918

Say you have a map

Map<String, List<Object>> map = new HashMap<>();

You can do this by calling the values() method and calling size() method for all the lists:

int total = 0;
Collection<List<Object>> listOfValues = map.values();
for (List<Object> oneList : listOfValues) {
    total += oneList.size();
}

Upvotes: 3

Related Questions