less than 1 minute read

Note for Advanced Java Programming

Generics

Generic method

public <T> List<T> arrayToList(T[] array, List<T>) {
    for (T t : array) {
        list.add(t);
    }
    return list;
}

Using varargs

void print(String... items) {
    for (int i = 0; i < items.length; ++i) {
        ...
    }
}

Using wildcards in generic programming

void print(List<? extends Animal> animals) {
    ...
}

Or <? Super Dog>

Collection

LinkedHashMap

LinkedHashMap<String, String> lhm = new LinkedHashMap<>(6, 0.75f, true);
// (capacity, reallocate boundary, order based on access)

Functional programming

functional interfaces

Interface only has one abstract method.

stream

List<String> lists;
// populate some data to lists

lists.stream().filter(str -> str.startsWith("A")
    .forEach(System.out.println);

The benefits about stream is it’s easy to convert to parallel operations, using parallelStream.

Modular Programming

A module-info.java is used to describe module, it should be in the root folder of the codes. It used to export/import packages to/from other modules.

module calculator{
  exports com.example.math;
  requires math.util;
}

Comments