author-pic

Tristan Krass

Java Streams


Published on January 01, 1970
public List<Integer> doubling(List<Integer> nums) {
  return nums.stream()
              .map(num -> num * 2)
              .collect(Collectors.toList()
              );
}

ava Functional API

list.replaceAll(*lambda*) -- calls the lambda once for each item in the list, storing the result back into the list (or other type of collection). e.g. nums.replaceAll(n -> n * 2);

list.removeIf(*lambda*) -- calls the lambda once for each item in the collection, removing each item where the lambda returns true. e.g. nums.removeIf(n -> n < 0);

Simple Lambda Examples -- the data types are inferred from the context and from the type of the expression following the "->": n -> n * 2

Hello

n -> n * 2

-- takes Integer, returns Integer n -> n < 0 && n >= -10 -- takes Integer, returns boolean s -> s.length()  -- takes String, returns Integer s -> s.startsWith("hi") -- takes String, returns boolean

The Java stream system provides more complicated lambda features. The stream calls do not modify the original list, returning a new data structure of the results. Note that the boolean logic of filter() is the opposite of removeIf().

List nums = -something-;   nums = nums.stream()     .map(n -> n * 2)     .filter(n -> n >= 0)     .collect(Collectors.toList());

If you like it, share it!