Search code examples
javaguavamultimap

How to check value of Multimap is null or empty?


I am using Multimap from Guava as shown below since I can have same keys many times but with different values.

Multimap<Long, Plesk> map = ArrayListMultimap.create();
// populate data into map

// extract value from the above map
Collection<Plesk> plesk = map.get(id);

And now from the same multimap I am extracting a particular key value. Now how can I check whether plesk variable is null or not? After I print out my map variable, I see one entry as:

1100178=[null]

so plesk variable is getting value as [null] for id 1100178 and if I use below code to check for null and empty then it doesn't work:

if (CollectionUtils.isEmpty(plesk)) {
    // do stuff
}

What is the right way to check whether value of multimap is null or not?


Solution

  • The result of Multimap.get(key) is never null. If there are no values associated with that key, it returns an empty Collection.

    Your plesk collection appears to be a collection with a single element, and that single element is null.