Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to extend an apex set class?

Asked by Darsh K May 5, 2023 1.1K views 1 answer
Share

About this question

 Is there any way I can extend the Set class? I have numerous places in code where if a set has no items, I want to add all the items in another set, but if it does have items, I want to retain only the items in the other set.


This seemed like a good use case for extending the Set class, but Apex complains about the lack of < following>


public with sharing class CoreSet extends Set {
    public Boolean retainAllIfPopulated(Set other) {
        return this.size() > 0 ? this.retainAll(other)
                               : this.addAll(other);
    }
}

Is it possible to extend the built-in Set class?

Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.