Question

I'm using a MultiValueMap from Apache Collections, to collect different types of word (Nouns, Verbs etc) and I want to check that I have at least one of each word type before continuing.

The general outline is like so (after initiating the keys):

MultiValueMap wordMap = new MultiValueMap().decorate(new HashMap(), LinkedList.class);
while (wordMap.notAllEmpty()){
    wordMap.put(wordType,word)  // eg, Noun, Giraffe
}

But I don't have a method for notAllEmpty(). I tried .values().contains(null) but the empty map doesn't contain null. Likewiese .values().isEmpty() doesn't work as all values are put together.

Is there a succinct way to check for this condition, perhaps with a method from the API?

Was it helpful?

Solution

It sounds like your trying to continue with your loop until there is at least one value per key. This is going to open you to a large possibility of an infinite loop if your data set isn't complete. To get to the point though you need to look at the keys, not the values. If you know how many word types there are than you should probably use the following.

int totalWordTypes = 10;
while (wordMap.keySet().size() < totalWordTypes) {
   //...
}

Otherwise you'll need a collection of the word types you are looking for and use something like...

while (!wordMap.keySet().containsAll(wordTypesCollection)) {
   //...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top