Use this material alongside inheritance, streams, files and testing. These are high-value topics in college papers, placement tests and Core Java interviews: contracts of common object methods, generics, collection choice, complexity, exceptions, JDBC and language features.
1. Generics and type safety
Generics parameterize types so the compiler can catch incompatible values. Prefer List over a raw List. Generic type arguments cannot be primitive types; use wrappers such as Integer.
List names = new ArrayList<>();
names.add("Mira");
String first = names.get(0);
// names.add(42); // compile-time error
Know generic classes, generic methods, bounded type parameters such as <T extends Number>, and wildcards. ? extends T is useful for reading values as T; ? super T is useful when adding T values (PECS: Producer Extends, Consumer Super).
2. Collection choice and complexity
List preserves order and permits duplicates; Set models uniqueness; Map associates keys with values. ArrayList is strong for indexed reads, LinkedList supports deque operations, HashSet/HashMap usually offer average constant-time basic operations, and TreeSet/TreeMap keep sorted order.
| Structure | Typical use | Typical cost |
|---|
| ArrayList | Indexed access, append | get O(1); append amortized O(1); middle insert O(n) |
| HashMap / HashSet | Lookup by key / uniqueness | Average O(1), subject to hashing and implementation |
| TreeMap / TreeSet | Sorted keys / values | O(log n) basic operations |
| ArrayDeque | Queue or stack operations | Amortized O(1) at ends |
3. equals() and hashCode()
When overriding equals, also override hashCode. Equal objects must have equal hash codes; unequal objects may share a hash. Hash-based collections use these methods to find logical matches.
final class User {
private final int id;
User(int id) { this.id = id; }
@Override public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
return id == ((User) o).id;
}
@Override public int hashCode() {
return Integer.hashCode(id);
}
}For newer Java versions, pattern matching may simplify type checks, but use syntax supported by your exam's target version.
4. Comparable vs Comparator
Comparable defines a type's natural ordering with compareTo. Comparator defines an external or alternate ordering. Comparators are useful for sorting by different fields.
List names = new ArrayList<>(
List.of("Zoe", "Ana", "Mira"));
names.sort(Comparator.naturalOrder());
names.sort(Comparator.comparingInt(String::length));
Comparison should be consistent and avoid subtraction-based comparators that can overflow. Use Integer.compare(a, b) for integer comparison.
5. Exceptions, immutability and resources
Checked exceptions must be caught or declared; unchecked exceptions derive from RuntimeException. Do not swallow exceptions silently. Custom exceptions should communicate a meaningful failure. Immutable objects are easier to reason about and safer to share.
try (BufferedReader reader = Files.newBufferedReader(path)) {
String line = reader.readLine();
System.out.println(line);
} catch (IOException e) {
System.err.println("Could not read file: " + e.getMessage());
}Try-with-resources closes AutoCloseable resources. finally is for cleanup that must happen whether or not an exception occurs, though resources are usually better managed with try-with-resources.
6. JDBC essentials
JDBC connects Java code to relational databases. Know Connection, PreparedStatement, ResultSet, transactions and SQL exceptions. Use placeholders instead of concatenating untrusted input into SQL.
String sql = "SELECT name FROM student WHERE id = ?";
try (Connection c = dataSource.getConnection();
PreparedStatement ps = c.prepareStatement(sql)) {
ps.setInt(1, studentId);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) System.out.println(rs.getString("name"));
}
}Driver configuration and DataSource setup depend on the environment. For transactions, understand commit, rollback and auto-commit.