OO Programming with Java


Lists & Iterators

  1. Variable arguments (Varargs)
    A program having an array as parameter is similar to a program having 0 to N parameter(s), i.e. a variable number of arguments, what conduces to equivalent notations:
void printAll(String[] vs) { ... }                  // definition
printAll(new String[]{"this","is","an","example"}); // use
// is equivalent to:
void printAll(String ... vs);
printAll("this","is","an","example");
  1. Reification of algorithms/functions
    'Reification' means 'to transform into an object' and mathematical function (e.g. Comparator) can be reified into:
interface Function<T,R> { // java.util.function
  public R apply(T x); }

Function<Double,Double> f = new Function<Double,Double>() {
      public Double apply(Double x) { return 2.0*x+1.0; }};

System.out.println( f.apply(2.0) );
  1. Using functional programming
    Finally, a comparator is a function and can be written as follow:
lst.sort((o1,o2)->o2.compare(o1)); 
System.out.println( lst );