Search code examples
javacollectionsguavamultimap

Guava Multimap containsEntry issues


Below I have some code that adds two entries to the first multimap and a single entry to the second, then compares the two. Since the keys are the same "Dec" (see output), why does it output false?

input

    Multimap<String, String> first = ArrayListMultimap.create();
    Multimap<String, String> second = ArrayListMultimap.create();
    first.put("Dec", "18");
    first.put("Dec", "12");
    second.put("Dec", "18");
    for (String key : second.keys()) {
            System.out.println(first.get(key));
        System.out.println(second.get(key));
        System.out.println(first.containsEntry(key, second.get(key)));
    }

output

    [18, 12]
    [18]
    false

Edit: For those who don't understand the answer below, second.get(key) will return a collection of strings (albeit only one) and of course a collection of strings =/= string


Solution

  • You're looking for an entry with a key of "Dec" and a value of "a collection containing just "18"". That entry doesn't exist in either first or second.

    Now if you use:

    System.out.println(first.containsEntry("Dec", "18"));
    

    then I expect that will print true. Basically you need to distinguish between an individual entry value and "the collection of entry values associated with a key".