Skip to content

Instantly share code, notes, and snippets.

@ru-rocker
Last active December 12, 2016 12:53
Show Gist options
  • Save ru-rocker/85957279d1e924adb33f8505b0c6dde2 to your computer and use it in GitHub Desktop.
Save ru-rocker/85957279d1e924adb33f8505b0c6dde2 to your computer and use it in GitHub Desktop.
Sample to populate Java Stream
//Stream from Collection
List<String> list = new ArrayList<>();
list.add("s1");
list.add("s2");
list.add("s3");
list.stream()
.map( s -> "From collection " + s)
.forEach(System.out::println);
//Stream from Arrays class
Arrays.stream(new Object[] { BigDecimal.ZERO, BigDecimal.ONE, BigDecimal.TEN })
.map( s -> "From array " + s)
.forEach(System.out::println);
//Creating stream from primitive value
//In this case from integer value, generate number: 1 to 10
IntStream.range(1, 11)
.mapToObj( s -> "From IntStream " + s)
.forEach(System.out::println);
//Creating stream from Stream interface
Stream.of(20.1d, 1.56d, 1.89d, 4.56d)
.map( s -> "From Stream.of " + s)
.forEach(System.out::println);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment