All subjects

Programming · Interview subject

Java Interview Questions

OOP, collections, streams and exception handling — the coding round core.

55 questions

Quick interview answer

HashMap = no order, O(1) average; LinkedHashMap = insertion (or access) order; TreeMap = sorted by key, O(log n), backed by a red-black tree.

Detailed explanation

HashMap allows one null key, TreeMap allows none (it must compare keys). Use LinkedHashMap when report ordering matters, TreeMap when you need range queries or sorted output.

java
1Map<String, Integer> counts = new HashMap<>();
2for (char c : "automation".toCharArray()) {
3 counts.merge(String.valueOf(c), 1, Integer::sum);
4}
5// sorted view
6Map<String, Integer> sorted = new TreeMap<>(counts);

Real-world example

Building a character-frequency report from test logs in a deterministic order.

Interview tip

Add the null-key nuance; it separates memorised answers from real understanding.

Next subjectSelenium