Skip to content

Instantly share code, notes, and snippets.

@cr0wst
Last active June 17, 2018 15:06
Show Gist options
  • Save cr0wst/6fe1d267388819957beee1a986cefb8d to your computer and use it in GitHub Desktop.
Save cr0wst/6fe1d267388819957beee1a986cefb8d to your computer and use it in GitHub Desktop.
Java Stream Example: Multiple all elements by 5
package net.smcrow.sandbox.streams;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MultiplyAllByFive {
private static final List<Integer> NUMBERS = List.of(1, 2, 3, 4, 5);
public static void main(String... args) {
System.out.println("Without Streams");
withoutStreams();
System.out.println("With Streams");
withStreams();
}
private static void withoutStreams() {
List<Integer> multiplied = new ArrayList<>();
for (Integer number : NUMBERS) {
multiplied.add(number * 5);
}
// To print the results you most likely used something like StringUtils.join, or Guava's Joiner
// Or you did messy things like this (not recommended because of the replacement:
String output = Arrays.toString(multiplied.toArray()).replace("[", "").replace("]", "");
System.out.println(output);
}
private static void withStreams() {
List<Integer> multiplied = NUMBERS.stream().map(i -> i * 5).collect(Collectors.toList());
String output = multiplied.stream().map(Object::toString).collect(Collectors.joining(", "));
System.out.println(output);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment