The labs on this site need JavaScript to run. The same content is written out below.
Fundamentals › Java
What do List and Map hold, and how do you get things out?
A container that holds several values is called a collection. A List holds values in a line, in the order you put them in, and you get them out by an index that starts at 0. A Map holds keys paired with values, and you get a value out by its key. In code, as in List<String> names = new ArrayList<>();, you write the kind of container on the left (the part in angle brackets is the kind of value it holds), and the new on the right creates an empty container. You do not write a size in the parentheses. The number of stored items grows as you put things in with add or put. In this lab you switch between List and Map, and the same shape of code is compiled and run, and you see what is printed when you get something that is stored, when you get something that is not stored, and when you put the same thing in once more. (The number of stored items is printed first so that you can see it even when the program stops while getting.)
Checked against real execution · Compiled 6 combinations with javax.tools on JDK 21, ran them as child java processes, and checked the standard output, the standard error, and the exit code
// Import java.util, which contains the collection classes
import java.util.*;
public class Main {
public static void main(String[] args) {
// List: a container that holds values in a line, in the order you put them in. It starts out empty
List<String> names = new ArrayList<>();
// Put in: add attaches the value to the end. Indexes are numbered from 0
names.add("kim");
names.add("lee");
// Count: size returns the number of stored values
System.out.println(names.size());
// Get: get returns the value at that index
System.out.println(names.get(0));
}
}
The compiler and runtime are JDK 21.0.12.1 (javac 21). All 6 combinations were checked against real runs: the same source as the code on the screen (identical line by line) was compiled with javax.tools and all of them compiled, and the compiled Main was run by starting the java of this JDK as a child process, and the standard output, the standard error, and the exit code were captured. What was checked is whether compilation succeeded, the lines of the standard output (the number stored and the value retrieved), and the exit code and the first line of the standard error (the combination that throws an exception had exit code 1 and the first line Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 2, followed by a stack trace with Main.main in the standard error, and the others had exit code 0 and an empty standard error). The facts that the step sentences rely on were checked by running separate source in the same way. In the List, "kim" and "lee" are stored at indexes 0 and 1 (the indexOf results were 0 and 1), the number stored stays 2 after a get, and putting the same "kim" in again stores it separately at index 2 (get(2) was kim, and the count was 3). In the Map, calling put with the same key returns the old value 20 and the value becomes 25 with the pairs staying at 2, and for the key park, which was not stored, get is null and containsKey is false. For the documentation, I opened the Java SE 21 API documentation myself. That indexes in a List start at 0 and that a List can hold the same value several times, that add attaches to the end, and that get throws an IndexOutOfBoundsException for an index out of range follow java.util.List and java.util.ArrayList; that an ArrayList grows its capacity by itself as you add follows java.util.ArrayList; that a Map cannot have two of the same key, that get returns null when there is no pair, and that put replaces the old value and returns it follow java.util.Map and java.util.HashMap; that IndexOutOfBoundsException is a subclass of RuntimeException follows java.lang.IndexOutOfBoundsException; and that the length of an array does not change after it is created follows section 10.2 of the Java Language Specification (Java SE 21). I opened these documents in a way that returned summaries of their content, so I did not read every word of the original text myself. That the code compiles even with a wrong index and that the exception is thrown at run time, and the exit code 1 and the text in the standard error, were not taken from the documentation but observed with this java. The code on the screen has exactly the shape that is run with java Main, and there is no package. The number stored is printed first and the line that gets comes after it so that, even in the combination that stops while getting, the number stored can be checked in the output. The dashed slots in the diagram (index 5, key park) are drawn to point to a position where nothing is stored, and they do not mean that a real List or Map has such a slot. Other methods of List and Map (remove, contains, and so on), ways to go through all the items, Set, how the order of retrieval is decided, and storing null as a value are not covered. The difference between ArrayList and LinkedList is covered in the in-depth lab "ArrayList and LinkedList", how HashMap finds a key in "Inside HashMap", and the order of the kinds of Set and Map in "Kinds of Set and Map".
Struck-through text was removed from the first version of the code; highlighted text was added.