Skip to content

Instantly share code, notes, and snippets.

@lcs-felix
Created August 29, 2020 12:48
Show Gist options
  • Save lcs-felix/03f67ec0a5d0644717aa46b3e0c08a4e to your computer and use it in GitHub Desktop.
Save lcs-felix/03f67ec0a5d0644717aa46b3e0c08a4e to your computer and use it in GitHub Desktop.
public class Main {
public static void main(String[] args) {
List<Integer> result = mymap(asList(1, 2, 3, 4, 5),
n -> n * n);
System.out.println(result);
}
static <T> List<T> mymap(List<T> list, Function<T, T> func) {
return list.stream()
.reduce(new ArrayList<T>(),
(a, b) -> {a.add(func.apply(b)); return a;},
(a, b) -> {a.addAll(b); return a;});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment