1

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]

4
  • 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, 2018 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, 2018 at 4:36
  • when i use set.contains it should display this duplicate right? but, its not!!
    – New
    Sep 5, 2018 at 4:38
  • Can you please share where you declare both set and how you are populating them. Sep 5, 2018 at 4:59

1 Answer 1

3

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.

2
  • 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, 2018 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, 2018 at 5:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .