Chapter 3 · Data

Lists and maps

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 19 · Concept

A list grows as you add to it

In this lesson you keep values in lists that grow, and in maps that find them by a key.

An array's length is fixed. An ArrayList grows as you add and shrinks as you remove. It lives in java.util.

ArrayList<String> items = new ArrayList<>();
items.add("bread");

ArrayList<String> reads "a list of String". The angle brackets name the elements' type, so javac refuses items.add(5). On the right, <> takes the type from the left.

import java.util.ArrayList;

public class Shopping {
    public static void main(String[] args) {
        ArrayList<String> items = new ArrayList<>();
        items.add("bread");
        items.add("milk");
        items.add("eggs");
        System.out.println(items);
        System.out.print(items.size() + " items, ");
        System.out.println("the first is " + items.get(0));
        System.out.print(items.contains("milk") + " ");
        System.out.print(items.indexOf("eggs") + " ");
        System.out.println(items.indexOf("tea"));
        items.remove("milk");
        System.out.println(items + " " + items.size());
        for (String item : items) {
            System.out.println("- " + item);
        }
    }
}
import java.util.ArrayList;

public class Unique {
    static ArrayList<String> unique(String[] names) {
        ArrayList<String> result = new ArrayList<>();
        // add each name that is not in result yet
        return result;
    }

    public static void main(String[] args) {
        String[] names = {"Ada", "Grace", "Ada",
                          "Alan", "Grace", "Ada"};
        ArrayList<String> once = unique(names);
        System.out.println(once);
        System.out.println(once.size() + " different names");
    }
}
import java.util.*;

public class Stock {
    public static void main(String[] args) {
        HashMap<String, Integer> stock = new HashMap<>();
        stock.put("pears", 2);
        stock.put("apples", 5);
        stock.put("apples", 7);
        System.out.print(stock.get("apples") + " ");
        System.out.println(stock.get("plums"));
        System.out.print(stock.getOrDefault("plums", 0) + " ");
        System.out.println(stock.containsKey("pears"));
        stock.put("pears", stock.get("pears") + 1);

        TreeMap<String, Integer> sorted = new TreeMap<>(stock);
        System.out.println(sorted.keySet());
        for (Map.Entry<String, Integer> e : sorted.entrySet()) {
            System.out.print(e.getKey() + ": ");
            System.out.println(e.getValue());
        }
    }
}
import java.util.HashMap;
import java.util.TreeMap;

public class Count {
    static HashMap<String, Integer> count(String[] words) {
        HashMap<String, Integer> counts = new HashMap<>();
        // add 1 to the count of each word
        return counts;
    }

    public static void main(String[] args) {
        String[] words = {"to", "be", "or", "not", "to", "be"};
        HashMap<String, Integer> counts = count(words);
        System.out.println(new TreeMap<>(counts));
        System.out.println("be: " + counts.get("be"));
    }
}
public class Main {
    public static void main(String[] args) {
        Gradebook book = new Gradebook();
        book.add("Grace");
        book.add("Ada");
        book.add("Grace");
        book.score("Ada", 7);
        book.score("Grace", 4);
        book.score("Ada", 5);
        book.score("Bob", 9);
        book.print();
        System.out.println("Best: " + book.best());
    }
}
public class Letters {
    public static void main(String[] args) {
        String word = "banana";
        // a map from each letter to its count,
        // printed in sorted order
    }
}
Terminal
Step 1 of 6A list that grows

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)