Skip to content

Instantly share code, notes, and snippets.

@ru-rocker
Last active December 15, 2016 12:01
Show Gist options
  • Save ru-rocker/e7281d5c364c6178775c1d6494ebdcd9 to your computer and use it in GitHub Desktop.
Save ru-rocker/e7281d5c364c6178775c1d6494ebdcd9 to your computer and use it in GitHub Desktop.
Sample using Java Stream
Random random = new Random();
List<Integer> list = new ArrayList<>();
//create a list of random integers using stream.
//Don't have to be like this, just to show off
//because we are talking about stream
List<Integer> list = IntStream.range(0, 20)
.map(i -> random.nextInt(100))
.boxed() //turns IntStream into Stream<Integer>
.collect(Collectors.toList()); //will be discussed in advance stream usage
System.out.printf("Generated list: %s \n", list);
//filter out all odd numbers from list
//then power by 2
list.stream()
.filter(i -> i % 2 == 0) //filter out the odd number
.map(i -> i * i) // Map function: power the even number by 2. Take it easy, I know Math.pow method
.forEach(System.out::println) // terminal operation. print out the result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment