Collections, generics, streams, records, exceptions, equals/hashCode
Practise these as flashcards. One or two lines each; the ones you miss come back first.
Start freeList.of() and why?Collections created with List.of() are immutable, so any attempt to add, remove, or modify elements will throw an UnsupportedOperationException. This is because List.of() returns an instance of an unmodifiable list introduced in Java 9.
equals() and hashCode() implementations, and what should you be aware of when using them in collections?Java records automatically generate equals() and hashCode() methods based on all component fields, ensuring value-based equality. When using records as keys in collections like HashMap or HashSet, this behavior ensures correct lookup and uniqueness as long as the fields used are themselves properly comparable and hashable.
The JVM runs Java bytecode, the JRE includes the JVM and standard libraries for running Java apps, and the JDK adds development tools like the compiler and debugger. You need the JDK to develop Java, but only the JRE to run it.
The contract states that if two objects are equal according to equals(), they must have the same hashCode. This is important for correct behavior in hash-based collections like HashMap and HashSet.
The String pool is a special memory area where Java stores String literals. If you create a String literal, Java checks the pool first and reuses the object if it already exists, saving memory.
ArrayList uses a dynamic array and offers fast random access (O(1)), but slow insert/delete in the middle. LinkedList uses nodes and is fast for insert/delete (O(1)), but slow for random access (O(n)).
List.of() and why?Collections created with List.of() are immutable, so any attempt to add, remove, or modify elements will throw an UnsupportedOperationException. This is because List.of() returns an instance of an unmodifiable list introduced in Java 9.
HashMap stores entries in buckets based on hashCode. Collisions are handled by chaining (linked lists), and since Java 8, by tree nodes if collisions are high. It resizes automatically and aims for O(1) access, but performance can degrade if hashCode is poorly implemented.
Checked exceptions must be declared or caught, like IOException. Unchecked exceptions, like NullPointerException, extend RuntimeException and don't need to be declared. Checked exceptions are for recoverable errors, unchecked for programming mistakes.
Type erasure means generic type information is removed at runtime. For example, List<String> and List<Integer> are both just List at runtime. This limits certain operations, like using instanceof with generic types.
Optional is a container that may or may not hold a value. It helps avoid null checks and makes it clear when a value might be absent, reducing NullPointerExceptions and improving code readability.
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) { br.readLine(); }
Interfaces define contracts and can have default methods but no state. Abstract classes can have state and provide partial implementation. Use interfaces for API contracts and abstract classes for shared code and fields.
Wildcards allow flexible APIs. ? extends T means any subtype of T, useful for reading; ? super T means any supertype, useful for writing. They help make generic methods more reusable.
equals() and hashCode() implementations, and what should you be aware of when using them in collections?