skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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:

    Multimap<String, String> multimap = LinkedListMultimap.create();
    multimap.put("Name", "SomeName");
    multimap.put("ID", "");
    multimap.put("City", "SomeCity");
    Collection<String> contentsofID = multimap.get("ID");
    if (contentsofID.toString().equals("[]")) {
        // This means that there is no value in it
        // Then perform what you want
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search