View on GitHub

wiki

Java

Data structures

When creating an object which is inherited from an abstract type (e.g., Map), make sure to initialize using the abstract class, instantiate (w/ ‘new’) with the concrete type (e.g., HashMap). This allows the flexibility of using, for example, a Map as a parameter, but being able to pass either a TreeMap or a HashMap in. Always use the highest-level interface possible.

Watch out for terms like “returns a view” or “backed by”. For instance, the sublist function, which returns part of an array, doesn’t return a copy of part of the array, it returns a “view” of part of the array. This is faster, but watch for mutability/concurrency issues.

Program structure

Classpaths

File types

Java streams and readers

Iterating through hashes

// Just the keys:
for (String key : map.keySet()) {
    // ...
}

// Just the values:
for (Object value : map.values()) {
    // ...
}

// Keys and values:
for (Map.Entry<String, Object> entry : map.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    // ...
}