OO Programming with Java


Advanced Concepts

map already exists in Stream class:

import java.util.*;
public class Funs {
  public static void main(String[] args) {
    List<Integer> xs = new ArrayList<Integer>();
    xs.add(-1); xs.add(-2); xs.add(-1); xs.add(0); xs.add(1);

    List<Integer> ys = xs
      .stream()
      .map(x->(x>=0)?x:-x)
      .toList();

    System.out.println(ys);
    // alternatively: ys.stream().forEach(System.out::println);
  }
}

Stream proposes other usefull functions to transform, select elements, etc. from collections.


10 - 17