Collections in Salesforce Apex: List, Set, Map and List to String

Read partners[0] from a query that returned no rows and Apex stops with System.ListException: List index out of bounds: 0. Ask a map for a key it does not have, add 1 to the answer, and you get a NullPointerException one line later.

Both bugs come from treating a List, a Set and a Map as if they behaved the same. We built one small class on five partner accounts at Ridgeline Supply in a Winter ’27 Developer Edition org, ran it, tested it, and broke it on purpose, so every output below is real.

By the end you will know which of the collections in salesforce apex to reach for, how to group records in a Map, and how to turn any list into a clean string with String.join.

This Tutorial Covers: