Kotlin
Java

Lists and Type Parameters
Java

Created By: Geoffrey Challen
/ Updated: 2021-09-13

Today we'll examine how to use Java's lists. We've stored sequential data before in an array, but lists are more flexible and more suited to certain tasks.

Lists

Like arrays, lists are an ordered data structure. However, unlike arrays, the size of a list can change as the program runs. This makes them much more appropriate for solving certain problems.

Java has built-in lists. Let's see how they work! (Feel free to open up the documentation as you follow along...)

Introduce typed Java lists. Point out that there are multiple list implementations, but we'll be using ArrayLists to solve problems below. Introduce the diamond operator.

List Creation and Initialization

Like arrays, Java Lists store sequential data. However, the syntax for using them is a bit different. Let's review the array operations we're familiar with and show how they map onto Listss.

First, let's show how we create an array and List, and check their size:

Note that with the List, unlike the array we do not need to specify a size when we create the List. This is because the size of a List can change!

Next up, how do we initialize a list with a set of values and print its contents? This turns out to be a bit harder than you would like with Java Lists, and requires a separate import statement:

List Get and Set

Now that we have a List with a few values, how do we access them and modify them? With arrays we used bracket notation, but Lists work differently:

List Add and Remove

Now we get to something that arrays can't do—modify their length! Java Lists provide both an add and a remove method. Let's explore how they work:

Discuss both add and remove by index, including adding at the end (no index) and to any point inside the list, and remove.

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

List<String> list = new ArrayList<>(Arrays.asList("1", "2", "4"));
System.out.println(list);

Boxing and Boxing Classes

Before we go on, a somewhat dull but important digression.

In the examples above we've seen how to create List of Strings:

But how would we create a list of int values? This doesn't seem to work:

The problem here is that Java lists have to store objects, and if you remember from early in the course, int (and long and boolean, etc.) are primitive types. So we can't store them in Lists. Uh-oh.

Happily, there's a simple solution. For each primitive type Java provides a boxing class that is a object but can store an instance of a primitive type. The boxing classes have capitalized names that are usually the same as the primitive type, with two exceptions:

  • byte -> Byte
  • short -> Short
  • int -> Integer
  • long -> Long
  • float -> Float
  • double -> Double
  • char -> Character
  • boolean -> Boolean

Thrilling, I know. But, happily, this now works:

Autoboxing

Under certain conditions Java will automatically box (primitive to object) and unbox (object to primitive) values for you:

But not always. In particular, arrays are not automatically boxed:

Unlike their primitive types, boxing types can also be null:

Type Parameters

The syntax that we introduce above is our first example of a Java type parameter:

The <String> on the left tells Java that this List variable will store Strings. And the <String> on the right tells Java to create a new ArrayList that will store Strings. If we wanted to store ints, we'd use <Integer> instead, for reasons that we explain below.

Usually these two types are the same, and so we can use the diamond operator for convenience:

It allows us to omit the String on the right side of this common expression, and saves us a few keystrokes.

Why do lists require a type parameter? It's so that we can tell Java what we are going to put in them! Once we do, Java will help us avoid common mistakes:

These mistakes are also caught before you program is run, at a step called compilation that we'll explore later this week.

Type Parameters in Documentation

It's important to understand how to identify type parameters when examining Java documentation. Let's do that together next.

Provide an overview of the List documentation, pointing out the type parameters.

List v. ArrayList

In the examples above we've used two imports: List and ArrayList:

This may seem a bit mysterious at the moment. And we won't be able to fully explain this until a bit later. But there is a difference between the two, and a good reason that we use both and not just ArrayList, which some of you may have seen in previous courses. We'll return to this topic later this semester.

Untyped Lists

We will almost always be using type parameters with our lists! This help make them safer, since we are telling Java what we will put in and take out of the List.

However, Java will let you create an untyped List, and this is what will happen if you omit the <String> or <Integer> type parameter. Let's discuss some of the problems that this can cause:

Discuss untyped lists and the input / output problems they create.

Overall we will avoid using bare or untyped lists and always provide type parameters when we use a List in our code.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough