function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ashu sharma 38Ashu sharma 38 

How to count the size of two maps

Hi,

As I have two maps 
Map 1---Program code and ContactId
Map 2---Program Code and Program term.


Now how to get the count of uniques program code from these two map.

Any idea.
Nayana KNayana K
Just a take a set. Set cannot contain a duplicate.
Eg:
Set<String> setUniqueCode = new Set<String>();
setUniqueCode.addAll(map1.keySet());
setUniqueCode.addAll(map2.keySet());

setUniqueCode will now contain all the unique codes. No dupes.
 
CarlosLimaCarlosLima
It will depend on what outcome you want, is Program Code the primary key? 

If so, duplicate keys are not allowed on a map so that you might set the ContactId from your contact SObject as the key in your first map, and for the second one, another key where it can be used as unique as well.
Contact example = new Contact(); 
Contact example2 = new Contact(); 

Map<Id, Contact> mapContacts = new Map<Id, Contact>(); 
mapContacts.put(example.Id, example); 
mapContacts.put(example2.Id, example2); 

Integer mSize = mapContacts.size(); 

//checking on test class system.assertEquals(2, mSize);

Otherwise, you might loop through those maps and add both in a unique set and then pick up the size.
Ashu sharma 38Ashu sharma 38
Hi ,

Okay...thanks
I am trying the steps.
Adheesh GuptaAdheesh Gupta
Hi Ashu,

A map consists of key-value pairs where keys uniquely can be mapped to one or many values, so the keyset itself is unique (without any duplicates).

​To get the count of map, you can use the size() function which will return you the number of key-value pairs in the map as below:

Interger count = map.size();

Thanks,
Adheesh