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
Puspendra SinghPuspendra Singh 

map having duplicate keys

Hello, I am new to APEX, Can someone tell me if this is possible to have duplicate Key in a MAP. I am adding element from a list to a map and when i print my map, i see duplicate value of the key. Here is how my map and its data items are defined.

public class ZTECase
    {
        public string OracleCaseNumber{get;set;}
    }
public class ZTEContact
    {
        public string FirstName{get;set;}
        public string LastName{get;set;}
        public string EmailAddress{get;set;}  
}

  map<ZTECase,ZTEContact> mapZTEData=new map<ZTECase,ZTEContact>
                      ZTECase objZTECase=new ZTECase();    
                      ZTEContact objZTEContact=new ZTEContact();    

In this map, we are adding  OracleCaseNumber and Contact info from Oracle Service Cloud. When i am printing this map post my addition, i see duplicate OracleCaseNumber which is my Key in the map ZTECase. Please suggest
 
Best Answer chosen by Puspendra Singh
Amit Chaudhary 8Amit Chaudhary 8

Please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm
  • Map keys and values can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
  • Uniqueness of map keys of user-defined types is determined by the equals and hashCode methods, which you provide in your classes. Uniqueness of keys of all other non-primitive types, such as sObject keys, is determined by comparing the objects’ field values.
  • Map keys of type String are case-sensitive. Two keys that differ only by the case are considered unique and have corresponding distinct Map entries. Subsequently, the Map methods, including put, get, containsKey, and remove treat these keys as distinct.

NOTE:- Map key can not be duplicate

You are getting duplicate value because you are using Class as Key.
map<ZTECase,ZTEContact> mapZTEData=new map<ZTECase,ZTEContact>();

If you will add string OracleCaseNumber as KEY then you will not get duplicate KEY like below
map<string ,ZTEContact> mapZTEData=new map<string ,ZTEContact>();

 

All Answers

Raj VakatiRaj Vakati
is is possible to have duplicate Key in a MAP -- No Map Key is always uniquer 

OracleCaseNumber  in varibale in your code so you can duplicates  -- > if you can set the 
OracleCaseNumber ​​​​​​​ as key then dnt be any dupllicate key 



in the below case

  map<ZTECase,ZTEContact> mapZTEData=new map<ZTECase,ZTEContact>

your key will be refererence of the ZTECase .. if you dnt instialed another instance of the ZTECase then you dnt have duplicate 
ZTECase ins1 = new ZTECase() ; 
  map<ZTECase,ZTEContact> mapZTEData=new map<ZTECase,ZTEContact>
mapZTEData.put(ins1 ,... ) ;
mapZTEData.put(ins1 ,... ) ;



Now you map can have oly one key form above code


ZTECase ins1 = new ZTECase() ; 
ZTECase ins2 = new ZTECase() ; 

  map<ZTECase,ZTEContact> mapZTEData=new map<ZTECase,ZTEContact>
mapZTEData.put(ins1 ,... ) ;
mapZTEData.put(ins2 ,... ) ;
Now you map can have two keys  form above code
 
Amit Chaudhary 8Amit Chaudhary 8

Please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm
  • Map keys and values can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
  • Uniqueness of map keys of user-defined types is determined by the equals and hashCode methods, which you provide in your classes. Uniqueness of keys of all other non-primitive types, such as sObject keys, is determined by comparing the objects’ field values.
  • Map keys of type String are case-sensitive. Two keys that differ only by the case are considered unique and have corresponding distinct Map entries. Subsequently, the Map methods, including put, get, containsKey, and remove treat these keys as distinct.

NOTE:- Map key can not be duplicate

You are getting duplicate value because you are using Class as Key.
map<ZTECase,ZTEContact> mapZTEData=new map<ZTECase,ZTEContact>();

If you will add string OracleCaseNumber as KEY then you will not get duplicate KEY like below
map<string ,ZTEContact> mapZTEData=new map<string ,ZTEContact>();

 
This was selected as the best answer
Puspendra SinghPuspendra Singh
Thank You Amit, your suggestion solve my problem.