Intermediate 25 min read

Custom Types in Collections: Wrapper Classes

By the end of this lesson, you'll be able to:

  • Build a List, Set, or Map of a custom class rather than a primitive type
  • Explain what a wrapper class is and why it's used with collections
  • Combine several related fields into one object stored inside a collection

Prerequisites: "Choosing List vs Set vs Map"

The limitation of primitive collections

List<String> productNames = new List<String>{'Widget', 'Gadget'};
List<Decimal> productPrices = new Decimal[]{49.99, 89.99};

Two separate lists, kept in sync by position only — fragile, since sorting or filtering one list without the other would silently misalign productNames[0] with the wrong price. Nothing enforces that these two lists stay related.

A wrapper class holds related data together

public class ProductSummary {
    public String name;
    public Decimal price;

    public ProductSummary(String name, Decimal price) {
        this.name = name;
        this.price = price;
    }
}

List<ProductSummary> products = new List<ProductSummary>{
    new ProductSummary('Widget', 49.99),
    new ProductSummary('Gadget', 89.99)
};

for (ProductSummary p : products) {
    System.debug(p.name + ': R' + p.price);
}

This is exactly Module 10's class-and-object pattern, now storing custom objects inside a List — each ProductSummary keeps its own name and price permanently together, with no risk of two separate lists drifting out of sync.

A real business example: Support Ticket Dashboard

public class TicketSummary {
    public String subject;
    public String priority;
    public Integer daysOpen;

    public TicketSummary(String subject, String priority, Integer daysOpen) {
        this.subject = subject;
        this.priority = priority;
        this.daysOpen = daysOpen;
    }
}

List<TicketSummary> openTickets = new List<TicketSummary>{
    new TicketSummary('Login failure', 'High', 2),
    new TicketSummary('Slow report load', 'Medium', 5)
};

A dashboard displaying open support tickets needs several fields per ticket shown together — exactly the case where a List<sObject> (a real Salesforce record) or a custom wrapper class like TicketSummary (useful when the data doesn't map cleanly to one object) both outperform several parallel primitive lists.

Exercise

Write a wrapper class EmployeeSummary with name and yearsOfService fields, then build a List<EmployeeSummary> with two employees.

Show hint

Give EmployeeSummary a constructor accepting both fields.

APEX

Custom Types in Collections: Wrapper Classes Quiz

1. What problem does a wrapper class solve compared to using several parallel primitive Lists?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

A collection isn't limited to primitives — storing a custom class in a List, Set, or Map (a "wrapper class" pattern) lets each item carry several related pieces of data together.