18What are generics and what is type erasure?
Generics let code express type-safe containers and APIs, such as List<String>. At compile time, the compiler checks types and inserts casts where needed. At runtime, most generic type information is erased, so List<String> and List<Integer> share the same runtime class. This is why you cannot directly create new T(), use primitive type parameters, or reliably check instanceof List<String>.
List<String> names = new ArrayList<>();
names.add("Alice");
List<? extends Number> producer;
List<? super Integer> consumer;
19What is the Java Collections Framework?
The Collections Framework is a set of interfaces and implementations for storing and processing groups of objects. Core interfaces include List, Set, Queue, Deque and Map. Implementations include ArrayList, LinkedList, HashSet, TreeSet, HashMap, TreeMap and PriorityQueue. Choose a collection by access pattern, ordering requirements, uniqueness, lookup complexity, memory overhead and concurrency needs.
List<String> list = new ArrayList<>();
Set<Long> ids = new HashSet<>();
Map<Long, User> usersById = new HashMap<>();
Deque<String> queue = new ArrayDeque<>();
20How does HashMap work?
HashMap stores entries in buckets selected by a hash of the key. For lookup, it calculates the hash, finds the bucket and compares keys with equals. Collisions are handled inside the bucket; in modern Java, long chains can be treeified into a balanced tree when conditions are met. HashMap allows one null key and null values, is not thread-safe, and can resize when the load factor threshold is exceeded.
Map<Long, User> users = new HashMap<>();
users.put(user.id(), user);
User found = users.get(user.id());
21What is the difference between HashMap, LinkedHashMap, TreeMap and ConcurrentHashMap?
HashMap provides fast unordered access and is not thread-safe. LinkedHashMap preserves insertion order or access order and is useful for LRU-like caches. TreeMap keeps keys sorted by natural order or Comparator and has O(log n) operations. ConcurrentHashMap is designed for concurrent access with segmented/internal synchronization, does not allow null keys or values, and is preferable to synchronizedMap under load.
map.computeIfAbsent(key, ignored -> loadValue());
22What is the difference between ArrayList and LinkedList?
ArrayList is backed by an array. It provides fast indexed access, compact memory layout and efficient iteration, but insertion in the middle requires shifting elements. LinkedList is a doubly linked list. It has cheaper insertion if the node is already known, but indexed access is O(n), memory overhead is higher and cache locality is worse. In many real applications ArrayList is faster even when some insertions are involved.
23What is the difference between Comparable and Comparator?
Comparable defines the natural order inside the class via compareTo. Comparator is a separate object or lambda that defines external ordering. Comparable is useful when a type has one obvious default order. Comparator is better when there are multiple orders, such as by name, date or priority. compare and compareTo must be consistent and preferably compatible with equals when used in sorted sets or maps.
class User implements Comparable<User> {
private int age;
@Override
public int compareTo(User other) {
return Integer.compare(age, other.age);
}
}
Comparator<User> byName =
Comparator.comparing(User::getName);
Comparator<User> byAgeThenName =
Comparator.comparingInt(User::getAge)
.thenComparing(User::getName);