1

Just want to know how can we check whether map contains values from a list

I have a list

List<Pickvalues> = new List<Pickvalues>((Pickval1, Pickval2, Pickval3));

Now I need to check whether map contains values from above list, I want to check each and every value of list.

Can anyone help me out how to achieve this.

2
  • Checking the map keys or the map values?
    – Keith C
    Feb 5, 2021 at 10:35
  • 1
    @KeithC map keys only, if map.containskey(list.value)
    – Harish
    Feb 5, 2021 at 10:45

1 Answer 1

1

You can see if it contains all values by checking the key set:

if(myMap.keySet().containsAll(values)) {

Or, you can check if it contains any values:

Set<String> testValues = new Set<String>(values);
testValues.retainAll(myMap.keySet());
if(!testValues.isEmpty()) { // Set contains all found values

As a final resort, you could also just loop over every item in the list to see if the values is foun:

Boolean found = false;
for(String value: values) {
  if(found |= myMap.containsKey(value)) {
    break;
  }
}
if(found) { // Found at least one value

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .