Core Java APIs· 40 min

Collections and Generics

Choose List, Set, Map, Queue, and concurrent structures correctly while keeping generic APIs type-safe.

Java interview lesson
Question 5 · Not reviewed

Core APIs · data structures

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

Collections and Generics

Explain it aloud in your own words

Strong answer

Choose collections based on access patterns. List preserves order and allows duplicates, Set models uniqueness, Map models key/value lookup, and Queue models processing order. Generics move type errors to compilation and make APIs safer without needing casts.

Key points

  • HashMap provides average constant-time lookup when hashing is well distributed.
  • TreeMap and TreeSet maintain sorted order with logarithmic operations.
  • ArrayList is usually the default List because of locality and fast indexed access.
  • Generics are invariant: List<String> is not a List<Object>.

Interviewer looks for

Explain both the data-structure choice and the failure mode: ordering, duplicates, nulls, concurrency, memory, or worst-case complexity.

Java example

1Map<String, Integer> counts = new HashMap<>();
2counts.merge("java", 1, Integer::sum);
3List<String> names = List.of("Ada", "Grace");

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Basic

How do List, Set, Map, and Queue differ?

Strong answer

List models ordered values and allows duplicates, Set models uniqueness, Map models key-to-value association, and Queue models processing order. The interface should reflect the behavior the caller needs.

Q2Intermediate

How does HashMap generally find a value?

Strong answer

It hashes the key, maps the hash to a bucket, and compares keys using hashCode and equals. Good hashes give average constant-time lookup, while collisions and resizing affect performance.

Q3Intermediate

Why can a mutable key cause a HashMap lookup to fail?

Strong answer

If fields used by hashCode or equals change after insertion, the key may belong to a different logical bucket than the one where it was stored. The entry remains present but is no longer found using the mutated key.

Q4Intermediate

Explain PECS for Java generics.

Strong answer

PECS means Producer Extends, Consumer Super. Use ? extends T when reading values as T from a producer, and ? super T when writing T values to a consumer. Invariance keeps generic APIs type-safe.

Q5Intermediate

When would you use ConcurrentHashMap instead of HashMap?

Strong answer

Use ConcurrentHashMap when multiple threads access and update a map and the map's operations must be thread-safe. It does not make compound application logic atomic automatically; use compute or other atomic APIs where appropriate.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.