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?
2
Answers
The result of
Multimap.get(key)
is nevernull
. If there are no values associated with that key, it returns an emptyCollection
.Your
plesk
collection appears to be a collection with a single element, and that single element isnull
.As others have already said, the result of
multimap.get(key)
is never null and returns an empty collection if there are no values associated.From your question, I can understand that you want to check whether there is a value present or not and perform some action if there is no value found for a specific key.
I would do something like this: