Collections framework (ArrayList, HashMap)

Computer Science · Java

Question 1 of 10

0

    What number does this Java code print? import java.util.*; public class Main { public static void main(String[] args) { ArrayList<Integer> items = new ArrayList<>(); for (int i = 1; i <= 6; i++) { items.add(i * 6); } for (int i = 0; i < 4; i++) { items.remove(0); } System.out.println(items.size()); } }

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Collections framework (ArrayList, HashMap) explained

    The Java Collections Framework provides ready-made data structures. ArrayList stores an ordered, resizable list of items, and HashMap stores key-value pairs, where each key is used to look up its value. Both are in java.util and are generic, so you declare the element type in angle brackets.

    An ArrayList is created with new ArrayList<>(). Items are added with add, read with get(index), and counted with size(). Indexes start at 0, and get throws an IndexOutOfBoundsException if the index is out of range. A HashMap<String, Integer> stores values with put(key, value) and reads them with get(key). get returns null when the key is missing, so unboxing that result into an int would throw a NullPointerException. HashMap does not promise any iteration order. Because collections hold objects, an int is stored as the wrapper type Integer.

    ArrayList<String> names = new ArrayList<>();
    names.add("Ana");
    names.add("Ben");
    
    HashMap<String, Integer> ages = new HashMap<>();
    ages.put("Ana", 31);
    System.out.println(names.size() + " " + ages.get("Ana"));
    

    Worked example

    Trace the code above.

    1. The two add calls put "Ana" at index 0 and "Ben" at index 1, so size() returns 2.
    2. ages.put("Ana", 31) stores 31 under the key "Ana".
    3. ages.get("Ana") returns 31.
    4. The output is "2 31". ages.get("Cal") would return null, not 0.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      What number does this Java code print?

      import java.util.*;

      public class Main { public static void main(String[] args) { ArrayList items = new ArrayList<>(); for (int i = 1; i <= 4; i++) { items.add(i * 5); } for (int i = 0; i < 2; i++) { items.remove(0); } System.out.println(items.size()); } }

    • Medium

      What number does this Java code print?

      import java.util.*;

      public class Main { public static void main(String[] args) { HashMap<String, Integer> stock = new HashMap<>(); stock.put("lime", 1); stock.put("fig", 1); stock.put("lime", 7); System.out.println(stock.size()); } }

    • Hard

      What number does this Java code print?

      import java.util.*;

      public class Main { public static void main(String[] args) { ArrayList list = new ArrayList<>(List.of(9, 7, 8, 5)); list.remove(2); list.remove(Integer.valueOf(7)); System.out.println(list.get(0)); } }