In the Salesforce docs for parameterized types stands the following:

Lists, maps and sets are parameterized in Apex: they take any data type Apex supports for them as an argument. That data type must be replaced with an actual data type upon construction of the list, map or set. For example:

List<String> myList = new List<String>();

Just wondering what are they trying to say with this line of code? Like what is the actual data type?


In languages like Java or C++, we can create so-called "templates" and "parameterized classes." They generally look something like this:

// C++
template <class T> {
  T add(T a, T b) {
    return a + b;
// Java
class Pair<X, Y> {
  X value1;
  Y value2;
  Pair(X v1, Y v2) {
    value1 = v1;
    value2 = v2;

Where the various types (T, X, Y) represent placeholders for a specific data type, like Integer or String, or even custom classes you write yourself (so-called "user classes").

In Apex, we're allowed to use some built-in types, such as:

class Map<X, Y> {
  // implementation
class List<X> {
  // implementation
class Set<X> {
  // implementation

Notably, we can't create our own in Apex at this time (maybe in the future, hopefully). For now, just know that whatever you specify in the <T> becomes the specialization for that data type.

This allows the compiler to know that you can't put a String in a List of Integer values, for example. There are some weird exceptions with parameterization and subclasses, so be careful how you use them, but in general, just know that the compiler is automatically creating "specialized" classes on-the-fly when you use parameterized classes.

  • I understand that part. I am specifically interested in what the docs are trying to say with that line of code. They say this That data type must be replaced with an actual data type upon construction of the list, map or set and then give an example. I want to see what that example has to do with the above sentence. What do they mean by actual data type in that line of code? – Michael Munta Feb 15 at 17:19
  • @MichaelMunta As in my example, T, X, and Y are placeholders for the real data type. They're saying you need to use String, Integer, MyCustomClass, etc as the real data type. – sfdcfox Feb 15 at 17:38
  • Ah, so supposing that X is any data type that can hold all kinds of types, they are trying to say that we cannot have such a type as a parameter, in other words we cannot have a list of for example Strings and Integers together? – Michael Munta Feb 18 at 11:05
  • @MichaelMunta e.g. Docs say, List<T>() Creates a new instance of the List class. A list can hold elements of any data type T.. You can't literally use T as the data type (unless you named a class T, I suppose);T needs to be replaced with Integer, String, etc. The docs aren't trying to be abstract or meta about things. That's all they're saying. If you make a LIst<Object>, you can have Integer and String in the same list (they are both Object). – sfdcfox Feb 18 at 11:21
  • So why even have specific types if we can always have List<Object>(or just List) and hold inside whatever we want? Are lists with specific types more optimized? Additionally, are there types other than the primitive types that do not inherit from Object so they wouldn't fall into this category where Object could cover every single type? – Michael Munta Feb 18 at 11:30

Lists, Maps, and Sets are collections in Apex, just like we have in Java. These collections can be of any type.

Say, list of integer, set of strings.

The parameterized means that the Lists, Maps, and Sets accept the parameter that defines what type of data these collections are going to store. As the apex is statically types language you need to mention the type of data you are going to store in the collection. That parameter defines the type of data that you can store.

For example

Map<String, Integer>

So you can't store an Integer in the List<String> and vice versa.

Also, note that these types can be nested as well. Eg.

  • I see a downvote, anything wrong with my post? I would like to know that. Thanks. – Rahul Gawale Feb 16 at 5:30

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.