You need to sign in to do that
Don't have an account?

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
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
Please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm
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
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
Please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm
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>();