@Test public void mapExampleParallel() { Stream<Integer> parallelStream = Stream.of(1, 2, 3).parallel(); List<Integer> values = StreamExercises.map(parallelStream, x -> x + 1); assertEquals(Arrays.asList(2, 3, 4), values); }
public static <T, R> List<R> map(Stream<T> stream, Function<T, R> mapper) { return stream.reduce(new ArrayList<>(), (acc, value) -> { ArrayList<R> result = new ArrayList<>(); result.addAll(acc); result.add(mapper.apply(value)); return result; }, (left, right) -> { ArrayList<R> result = new ArrayList<>(); result.addAll(left); result.addAll(right); return result; }); }
StreamExercises { public static <T, R> List<R> map(Stream<T> stream, Function<T, R> mapper) { return stream.reduce(new ArrayList<>(), (acc, value) -> { ArrayList<R> result = new ArrayList<>(); result.addAll(acc); result.add(mapper.apply(value)); return result; }, (left, right) -> { ArrayList<R> result = new ArrayList<>(); result.addAll(left); result.addAll(right); return result; }); } }
StreamExercises { public static <T, R> List<R> map(Stream<T> stream, Function<T, R> mapper) { return stream.reduce(new ArrayList<>(), (acc, value) -> { ArrayList<R> result = new ArrayList<>(); result.addAll(acc); result.add(mapper.apply(value)); return result; }, (left, right) -> { ArrayList<R> result = new ArrayList<>(); result.addAll(left); result.addAll(right); return result; }); }  }
StreamExercises { public static <T, R> List<R> map(Stream<T> stream, Function<T, R> mapper) { return stream.reduce(new ArrayList<>(), (acc, value) -> { ArrayList<R> result = new ArrayList<>(); result.addAll(acc); result.add(mapper.apply(value)); return result; }, (left, right) -> { ArrayList<R> result = new ArrayList<>(); result.addAll(left); result.addAll(right); return result; }); }  static int countBandMembersExternal(List<Artist> artists); static List<R> map(Stream<T> stream, Function<T, R> mapper); }
StreamExercises { public static <T, R> List<R> map(Stream<T> stream, Function<T, R> mapper) { return stream.reduce(new ArrayList<>(), (acc, value) -> { ArrayList<R> result = new ArrayList<>(); result.addAll(acc); result.add(mapper.apply(value)); return result; }, (left, right) -> { ArrayList<R> result = new ArrayList<>(); result.addAll(left); result.addAll(right); return result; }); }  static int countBandMembersExternal(List<Artist> artists); static List<R> map(Stream<T> stream, Function<T, R> mapper);  }