OO Programming with Java


Advanced Concepts

Record are syntactic sugars to build classes more easilly:

  record Pair<X,Y>(X x,Y y) {};
  
  public static void main(String[] args) {
    List<Pair<String,String>> ps;
    ps = Arrays.asList(
      new Pair("bill","IR"),
      new Pair("kate","AS"),
      new Pair("john","IR"));
    
    System.out.println( // what's this SQL query ?
      ps.stream()
      .filter(p->p.y=="IR")
      .map(p->p.x)
      .toList());
  }

Remark. It is possible to use method reference written :: for simplicity:

      ps.stream()
      .filter(p->p.y=="IR")
      .map(Pair::x)
      .forEach(System.out::println);

The forEach replaces map because println is not a function (void type).


14 - 17