how can I check if there's element in my arraylist that is not in the hashmap?

  • A+
Category:Languages

I have a List<String> in java and it contains some Strings.

I also have a hashmap with String values and I would like to check if there's any element in my List that is not in the Hashmap. This is the code I wrote:

List<String> someStrings = fetchData(); if (someStrings.stream().noneMatch(s -> myHashMap.containsValue(s))) {     return false; }  return true; 

But it does not work properly. Can you help me with that?

 


Given that your condition is

if there's any element in my List that is not in the Hashmap

you can use anyMatch while iterating over the list of elements to check if any of them is not present in the hashmap values.

return someStrings.stream().anyMatch(val -> !myHashMap.containsValue(val)) 

A simpler check though could also be using containsAll over the Collection of values :

return myHashMap.values().containsAll(someStrings); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: