I am trying to populate this Set (buildingIds) with string values, but I am getting the error: Method does not exist or incorrect signature: void add(Object) from the type Set.

I am returning some json and iterating through it trying to exact the "building ids" into a Apex Set to ensure uniqueness.

If I dump out the values using: listingAttribute.get(attributeName) within my for loop I see the value for the building id at each iteration of the loop, but it errors when I try to add it to my set.

My code is below:

 Map<String, Object> jsonResults = (Map<String, Object>) JSON.deserializeUntyped(JSONResponse);
 Set<String> buildingIds = new Set<String>();

 List<Object> listBuildings = (List<Object>)jsonResults.get('buildings');

 for (Object buildingRecord: listBuildings ) {
    Map<String, Object> buildingAttributes = (Map<String, Object>)buildingRecord;

   for (String attributeName : buildingAttributes .keySet()) {

     if( attributeName == 'building_id') {
       buildingIds.add(buildingAttributes.get(attributeName)); // **errors here**

} // close outer for loop

How can I add the building Id to the set?

3 Answers 3


Each of these two approaches below should work:




The problem is you try to add an Object into a Set of string.


Change it to buildingIds.add((buildingAttributes.get(attributeName)).Id);

That should work.


Your map:

Map<String, Object> buildingAttributes ...

has a signature where the get method returns Object (as there are various types involved) but your set requires String. This is a case where a cast is used: you know that the value is in fact a String when the name is 'building_id' so you can (and must) downcast:

buildingIds.add((String) buildingAttributes.get(attributeName));

(If the values are Id values, better to use Set<String> buildingIds = new Set<Id>(); and downcast to (Id).)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.