Set.contains() not working

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












My set contains custom JSON elements. When i try to use set.contains() method to check if the set has the JSON element it fails.



MY JSON DATA:



public uJSON(Id id,String name, Boolean manager,Id managerId) 

this.id = id;
this.name = name;
this.manager = manager;
this.managerId = managerId;



My first Set set<uJSON> sortedU has uJSON elements in it.
My second Set set<uJSON> secondSet has UJSON elements too.



How to check if the element in secondSet exists in sortedU set or not. I have tried using contains method but it fails. (I am 100% sure there are duplicate elements)



APEX Code



for(uJSON reps : secondSet)

if(sortedU.contains(reps))
system.debug('Dupicate user found: '+reps);

else
// need to add this user to final list




My sortedU contains




uJSON:[id=00541000008bbbbAAA, manager=false,
managerId=00541000009ggggAAA, name=** Matt Tom, sortBy=name],
uJSON:[id=00541000008aa6aAAA, manager=false,
managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name]




and secondSet also contains




uJSON:[id=00541000008aa6aAAA, manager=false,
managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name]











share|improve this question























  • note: If the set contains String elements, the elements are case-sensitive. Two set elements that differ only by case are considered distinct.
    – glls
    Sep 5 at 4:33










  • sortedU contains uJSON:[id=00541000008bbbbAAA, manager=false, managerId=00541000009ggggAAA, name=** Matt Tom, sortBy=name], uJSON:[id=00541000008aa6aAAA, manager=false, managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name] and secondSet also contains uJSON:[id=00541000008aa6aAAA, manager=false, managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name]
    – New
    Sep 5 at 4:36











  • when i use set.contains it should display this duplicate right? but, its not!!
    – New
    Sep 5 at 4:38










  • Can you please share where you declare both set and how you are populating them.
    – Tushar Sharma
    Sep 5 at 4:59
















up vote
1
down vote

favorite












My set contains custom JSON elements. When i try to use set.contains() method to check if the set has the JSON element it fails.



MY JSON DATA:



public uJSON(Id id,String name, Boolean manager,Id managerId) 

this.id = id;
this.name = name;
this.manager = manager;
this.managerId = managerId;



My first Set set<uJSON> sortedU has uJSON elements in it.
My second Set set<uJSON> secondSet has UJSON elements too.



How to check if the element in secondSet exists in sortedU set or not. I have tried using contains method but it fails. (I am 100% sure there are duplicate elements)



APEX Code



for(uJSON reps : secondSet)

if(sortedU.contains(reps))
system.debug('Dupicate user found: '+reps);

else
// need to add this user to final list




My sortedU contains




uJSON:[id=00541000008bbbbAAA, manager=false,
managerId=00541000009ggggAAA, name=** Matt Tom, sortBy=name],
uJSON:[id=00541000008aa6aAAA, manager=false,
managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name]




and secondSet also contains




uJSON:[id=00541000008aa6aAAA, manager=false,
managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name]











share|improve this question























  • note: If the set contains String elements, the elements are case-sensitive. Two set elements that differ only by case are considered distinct.
    – glls
    Sep 5 at 4:33










  • sortedU contains uJSON:[id=00541000008bbbbAAA, manager=false, managerId=00541000009ggggAAA, name=** Matt Tom, sortBy=name], uJSON:[id=00541000008aa6aAAA, manager=false, managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name] and secondSet also contains uJSON:[id=00541000008aa6aAAA, manager=false, managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name]
    – New
    Sep 5 at 4:36











  • when i use set.contains it should display this duplicate right? but, its not!!
    – New
    Sep 5 at 4:38










  • Can you please share where you declare both set and how you are populating them.
    – Tushar Sharma
    Sep 5 at 4:59












up vote
1
down vote

favorite









up vote
1
down vote

favorite











My set contains custom JSON elements. When i try to use set.contains() method to check if the set has the JSON element it fails.



MY JSON DATA:



public uJSON(Id id,String name, Boolean manager,Id managerId) 

this.id = id;
this.name = name;
this.manager = manager;
this.managerId = managerId;



My first Set set<uJSON> sortedU has uJSON elements in it.
My second Set set<uJSON> secondSet has UJSON elements too.



How to check if the element in secondSet exists in sortedU set or not. I have tried using contains method but it fails. (I am 100% sure there are duplicate elements)



APEX Code



for(uJSON reps : secondSet)

if(sortedU.contains(reps))
system.debug('Dupicate user found: '+reps);

else
// need to add this user to final list




My sortedU contains




uJSON:[id=00541000008bbbbAAA, manager=false,
managerId=00541000009ggggAAA, name=** Matt Tom, sortBy=name],
uJSON:[id=00541000008aa6aAAA, manager=false,
managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name]




and secondSet also contains




uJSON:[id=00541000008aa6aAAA, manager=false,
managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name]











share|improve this question















My set contains custom JSON elements. When i try to use set.contains() method to check if the set has the JSON element it fails.



MY JSON DATA:



public uJSON(Id id,String name, Boolean manager,Id managerId) 

this.id = id;
this.name = name;
this.manager = manager;
this.managerId = managerId;



My first Set set<uJSON> sortedU has uJSON elements in it.
My second Set set<uJSON> secondSet has UJSON elements too.



How to check if the element in secondSet exists in sortedU set or not. I have tried using contains method but it fails. (I am 100% sure there are duplicate elements)



APEX Code



for(uJSON reps : secondSet)

if(sortedU.contains(reps))
system.debug('Dupicate user found: '+reps);

else
// need to add this user to final list




My sortedU contains




uJSON:[id=00541000008bbbbAAA, manager=false,
managerId=00541000009ggggAAA, name=** Matt Tom, sortBy=name],
uJSON:[id=00541000008aa6aAAA, manager=false,
managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name]




and secondSet also contains




uJSON:[id=00541000008aa6aAAA, manager=false,
managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name]








json duplicate-management set contains






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 5 at 6:08









Reshma

2,53121222




2,53121222










asked Sep 5 at 4:31









New

205




205











  • note: If the set contains String elements, the elements are case-sensitive. Two set elements that differ only by case are considered distinct.
    – glls
    Sep 5 at 4:33










  • sortedU contains uJSON:[id=00541000008bbbbAAA, manager=false, managerId=00541000009ggggAAA, name=** Matt Tom, sortBy=name], uJSON:[id=00541000008aa6aAAA, manager=false, managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name] and secondSet also contains uJSON:[id=00541000008aa6aAAA, manager=false, managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name]
    – New
    Sep 5 at 4:36











  • when i use set.contains it should display this duplicate right? but, its not!!
    – New
    Sep 5 at 4:38










  • Can you please share where you declare both set and how you are populating them.
    – Tushar Sharma
    Sep 5 at 4:59
















  • note: If the set contains String elements, the elements are case-sensitive. Two set elements that differ only by case are considered distinct.
    – glls
    Sep 5 at 4:33










  • sortedU contains uJSON:[id=00541000008bbbbAAA, manager=false, managerId=00541000009ggggAAA, name=** Matt Tom, sortBy=name], uJSON:[id=00541000008aa6aAAA, manager=false, managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name] and secondSet also contains uJSON:[id=00541000008aa6aAAA, manager=false, managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name]
    – New
    Sep 5 at 4:36











  • when i use set.contains it should display this duplicate right? but, its not!!
    – New
    Sep 5 at 4:38










  • Can you please share where you declare both set and how you are populating them.
    – Tushar Sharma
    Sep 5 at 4:59















note: If the set contains String elements, the elements are case-sensitive. Two set elements that differ only by case are considered distinct.
– glls
Sep 5 at 4:33




note: If the set contains String elements, the elements are case-sensitive. Two set elements that differ only by case are considered distinct.
– glls
Sep 5 at 4:33












sortedU contains uJSON:[id=00541000008bbbbAAA, manager=false, managerId=00541000009ggggAAA, name=** Matt Tom, sortBy=name], uJSON:[id=00541000008aa6aAAA, manager=false, managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name] and secondSet also contains uJSON:[id=00541000008aa6aAAA, manager=false, managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name]
– New
Sep 5 at 4:36





sortedU contains uJSON:[id=00541000008bbbbAAA, manager=false, managerId=00541000009ggggAAA, name=** Matt Tom, sortBy=name], uJSON:[id=00541000008aa6aAAA, manager=false, managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name] and secondSet also contains uJSON:[id=00541000008aa6aAAA, manager=false, managerId=00541000008gf6zAAA, name=** Alicia Camry, sortBy=name]
– New
Sep 5 at 4:36













when i use set.contains it should display this duplicate right? but, its not!!
– New
Sep 5 at 4:38




when i use set.contains it should display this duplicate right? but, its not!!
– New
Sep 5 at 4:38












Can you please share where you declare both set and how you are populating them.
– Tushar Sharma
Sep 5 at 4:59




Can you please share where you declare both set and how you are populating them.
– Tushar Sharma
Sep 5 at 4:59










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










As mentioned in Using Custom Types in Map Keys and Sets, you need to override hashCode and equals in order to have Set work correctly with your custom class. For example:



public Integer hashCode() 
return System.hashCode(id+'♥'+name+'♥'+manager+'♥'+managerid);

public Boolean equals(Object other)
uJson cmp = (uJSON)other;
return cmp.id == id && cmp.name == name && cmp.manager && cmp.managerid == managerid;



Once properly implemented, contains should work as you expect.






share|improve this answer






















  • return statement is throwing an error saying "Method does not exist or incorrect signature: void hashCode(String) from the type String". Basically its not returning a integer value
    – New
    Sep 5 at 5:42










  • @New Yes, it should have been System.hashcode, not String.hashCode. It's been a while since I've used this.
    – sfdcfox
    Sep 5 at 5:49










  • Thank you @sfdcfox..It's Working now!!
    – New
    Sep 5 at 6:13










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "459"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f231234%2fset-contains-not-working%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










As mentioned in Using Custom Types in Map Keys and Sets, you need to override hashCode and equals in order to have Set work correctly with your custom class. For example:



public Integer hashCode() 
return System.hashCode(id+'♥'+name+'♥'+manager+'♥'+managerid);

public Boolean equals(Object other)
uJson cmp = (uJSON)other;
return cmp.id == id && cmp.name == name && cmp.manager && cmp.managerid == managerid;



Once properly implemented, contains should work as you expect.






share|improve this answer






















  • return statement is throwing an error saying "Method does not exist or incorrect signature: void hashCode(String) from the type String". Basically its not returning a integer value
    – New
    Sep 5 at 5:42










  • @New Yes, it should have been System.hashcode, not String.hashCode. It's been a while since I've used this.
    – sfdcfox
    Sep 5 at 5:49










  • Thank you @sfdcfox..It's Working now!!
    – New
    Sep 5 at 6:13














up vote
3
down vote



accepted










As mentioned in Using Custom Types in Map Keys and Sets, you need to override hashCode and equals in order to have Set work correctly with your custom class. For example:



public Integer hashCode() 
return System.hashCode(id+'♥'+name+'♥'+manager+'♥'+managerid);

public Boolean equals(Object other)
uJson cmp = (uJSON)other;
return cmp.id == id && cmp.name == name && cmp.manager && cmp.managerid == managerid;



Once properly implemented, contains should work as you expect.






share|improve this answer






















  • return statement is throwing an error saying "Method does not exist or incorrect signature: void hashCode(String) from the type String". Basically its not returning a integer value
    – New
    Sep 5 at 5:42










  • @New Yes, it should have been System.hashcode, not String.hashCode. It's been a while since I've used this.
    – sfdcfox
    Sep 5 at 5:49










  • Thank you @sfdcfox..It's Working now!!
    – New
    Sep 5 at 6:13












up vote
3
down vote



accepted







up vote
3
down vote



accepted






As mentioned in Using Custom Types in Map Keys and Sets, you need to override hashCode and equals in order to have Set work correctly with your custom class. For example:



public Integer hashCode() 
return System.hashCode(id+'♥'+name+'♥'+manager+'♥'+managerid);

public Boolean equals(Object other)
uJson cmp = (uJSON)other;
return cmp.id == id && cmp.name == name && cmp.manager && cmp.managerid == managerid;



Once properly implemented, contains should work as you expect.






share|improve this answer














As mentioned in Using Custom Types in Map Keys and Sets, you need to override hashCode and equals in order to have Set work correctly with your custom class. For example:



public Integer hashCode() 
return System.hashCode(id+'♥'+name+'♥'+manager+'♥'+managerid);

public Boolean equals(Object other)
uJson cmp = (uJSON)other;
return cmp.id == id && cmp.name == name && cmp.manager && cmp.managerid == managerid;



Once properly implemented, contains should work as you expect.







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 5 at 5:48

























answered Sep 5 at 5:04









sfdcfox

228k10176390




228k10176390











  • return statement is throwing an error saying "Method does not exist or incorrect signature: void hashCode(String) from the type String". Basically its not returning a integer value
    – New
    Sep 5 at 5:42










  • @New Yes, it should have been System.hashcode, not String.hashCode. It's been a while since I've used this.
    – sfdcfox
    Sep 5 at 5:49










  • Thank you @sfdcfox..It's Working now!!
    – New
    Sep 5 at 6:13
















  • return statement is throwing an error saying "Method does not exist or incorrect signature: void hashCode(String) from the type String". Basically its not returning a integer value
    – New
    Sep 5 at 5:42










  • @New Yes, it should have been System.hashcode, not String.hashCode. It's been a while since I've used this.
    – sfdcfox
    Sep 5 at 5:49










  • Thank you @sfdcfox..It's Working now!!
    – New
    Sep 5 at 6:13















return statement is throwing an error saying "Method does not exist or incorrect signature: void hashCode(String) from the type String". Basically its not returning a integer value
– New
Sep 5 at 5:42




return statement is throwing an error saying "Method does not exist or incorrect signature: void hashCode(String) from the type String". Basically its not returning a integer value
– New
Sep 5 at 5:42












@New Yes, it should have been System.hashcode, not String.hashCode. It's been a while since I've used this.
– sfdcfox
Sep 5 at 5:49




@New Yes, it should have been System.hashcode, not String.hashCode. It's been a while since I've used this.
– sfdcfox
Sep 5 at 5:49












Thank you @sfdcfox..It's Working now!!
– New
Sep 5 at 6:13




Thank you @sfdcfox..It's Working now!!
– New
Sep 5 at 6:13

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f231234%2fset-contains-not-working%23new-answer', 'question_page');

);

Post as a guest













































































這個網誌中的熱門文章

How to combine Bézier curves to a surface?

Carbon dioxide

Why am i infinitely getting the same tweet with the Twitter Search API?