Core Java · Chapter 26 of 48

Generics

By the end of this chapter you can write a class and a method that work for any type, limit that type when you need to, and accept lists of related types with wildcards. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Core Java
  • 6 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Core Java are free. This one comes with the all labs plan.

Why you need this

Ravi has three shelves behind the SBC counter: milk packets, bun trays and Parle-G. Yesterday a bun tray ended up on the milk shelf. He wants the software to model the shelves so that a mix-up is impossible.

With what you know so far, you would write one shelf class per kind of thing, the same code three times, or one shelf of Object that takes anything. The Object shelf accepts the bun tray without a word, and every take needs a cast that fails only at run time, in the rush.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Shelf.java: build a shelf that holds one type of thing, first in, first out

    In Shelf.java, write the three methods of the generic Shelf class. put(item) adds the item at the back and returns true, or returns false and changes nothing when the shelf already holds capacity items. take() removes and returns the item that has been on the shelf longest, and throws a NoSuchElementException when the shelf is empty. size() returns how many items are on the shelf, 0 for a new one. It works the same for any type, a Shelf of Integer too.

    For example, on a shelf of capacity 2, Toned milk and Full cream milk fit, Buffalo milk does not and the size stays 2; take() then returns Toned milk, then Full cream milk, and then throws.

  2. Task 2

    Pantry.java: pick an item or fall back

    In Pantry.java, make pick(items, index, fallback) return the item at index, or fallback when the index is outside the list: negative, or equal to the size or more. An empty list always gives the fallback. It works for a list of any type.

    For example, with SBC, NDLS and CSMT, index 1 returns NDLS and index 0 returns SBC; index 3 and index -1 return the fallback none; an empty list of Integer with fallback 0 returns 0.

  3. Task 3

    Pantry.java: find the largest of anything that can be compared

    In Pantry.java, make max(items) return the largest item by its natural order, the order its compareTo gives, for any Comparable type. An empty list throws a NoSuchElementException.

    For example, 40, 25, 50 and 10 give 50; -7, -3 and -10 give -3; NDLS, SBC and CSMT give SBC (strings compare alphabetically); a list holding only Bun maska gives Bun maska.

  4. Task 4

    Pantry.java: add up a list of any kind of number

    In Pantry.java, make total(amounts) return the sum of the numbers as a double. It accepts a List of Integer, a List of Double, or a List of Number holding both. An empty list returns 0.0.

    For example, 40 and 25 return 65.0; 12.5 and 7.5 return 20.0; 40 and 15.5 together return 55.5.

  5. Task 5

    Pantry.java: copy items into a list of a wider type

    In Pantry.java, make copyAll(from, to) add every item of from to the end of to, in order, and return how many items were copied. from is left unchanged. The list to may be of a wider type than from.

    For example, copying 40 and 25 into a List of Number holding 12.5 leaves it as 12.5, 40, 25 and returns 2, and the list of 40 and 25 is unchanged. Copying SBC, NDLS and CSMT into an empty List of Object returns 3; copying an empty list returns 0.

  6. Task 6 · interview question

    Interview: Pantry.java, predict what type erasure leaves at run time

    In Pantry.java, make predictErasure() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them (true or false in lower case, and class names in full).

    The check runs the same code and compares your list with what Java really printed.

What you will learn

  • Type parameters: a class or a method written once for "some type T", which each user fills in.
  • Bounds: T extends Comparable<T>, for a T that must be able to do something.
  • Wildcards: ? extends to read from a list of related types, ? super to write into one.
  • Type erasure: what is left of T when the program runs.

More in Core Java

Build "Generics" yourself

Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Core Java are free. This one comes with the all labs plan.