0

I have a map Map<String,Integer> occurenceCounter = new Map<String,Integer>();

occurenceCounter{M-1=1, M-2=4, M-3=1, M-6=1, M-29=5, M-13=1, M-90=2, M-112=8, M-5=1}

I want to check if map values contain only value 1 ( all key values are 1)

if it contains any other value other than 1. I want to run some specific logic

How can I do it ?

1 Answer 1

4

Consider the following:

Set<Integer> values = new Set<Integer>(occurenceCounter.values());

if (values.size() != 1 || !values.contains(1)) {
    System.debug('Not just 1');
}

This is quick and efficient because it uses Salesforce API features and not Apex loops. It does increase heap usage, briefly, but much better than iterating in Apex which will burn your CPU time (which is generally more of a consideration).

You must log in to answer this question.

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