Collections in Java — Java Technical Interview Questions & Answers

Collections in Java

Collections in Java

  • The Collections Framework gives ready-made structures for groups of objects. List: ordered, duplicates allowed — ArrayList. Set: unique elements — HashSet. Map: key → value — HashMap (Map is separate but part of the same story).
  • Prefer generics: List<String> so types stay safe. Pick by need: fast index get → ArrayList; uniqueness → HashSet; key lookup → HashMap. Don’t use a raw array for everything that must grow or shrink.
  • Draw List vs Set vs Map in ten seconds with one property each.
  • Let's take this on the board with one tiny Main class — no extra files. Collections in Java — output: 3. Three real names: Asha, Ravi, Neha. size() is 3. Start from main, go line by line, and stop at each print. That output is the proof for Collections.
  List  → order + duplicates   (ArrayList)
  Set   → unique only          (HashSet)
  Map   → key → value          (HashMap)

Technical Interview Tip: List / Set / Map — one line each.

Code Examples for Collections in Java

Example

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> names = new ArrayList<>();
    names.add("Asha");
    names.add("Ravi");
    names.add("Neha");
    System.out.println(names.size()); // 3
  }
}

Collections in Java — output: 3. Three real names: Asha, Ravi, Neha. size() is 3.

How to Explain Collections in Java to an Interviewer

List vs Set?

List: order + duplicates OK Set: unique

What is a Map?

key → value HashMap

Why generics?

Type safety List<String>