Set.contains() not working
Clash 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]
json duplicate-management set contains
add a comment |Â
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]
json duplicate-management set contains
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
add a comment |Â
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]
json duplicate-management set contains
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
json duplicate-management set contains
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
add a comment |Â
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
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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