package com.github.hathibelagal.streams; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class Examples { public static void main(String[] args) { // example1(); // example2(); // example3(); // example4(); // example5(); // example6(); } static void example1() { // Create an ArrayList List myList = new ArrayList(); myList.add(1); myList.add(5); myList.add(8); // Convert it into a Stream Stream myStream = myList.stream(); // Print the contents of the stream myStream.forEach(e -> System.out.println(e)); } static void example2() { // Create an array Integer[] myArray = {1, 5, 8}; // Convert it into a Stream Stream myStream = Arrays.stream(myArray); // Print the contents of the stream myStream.forEach(e -> System.out.println(e)); } static void example3() { String[] myArray = new String[]{"bob", "alice", "paul", "ellie"}; Stream myStream = Arrays.stream(myArray); Stream myNewStream = myStream.map(s -> s.toUpperCase()); String[] myNewArray = myNewStream.toArray(String[]::new); // Print the contents of the new array for(String s:myNewArray) System.out.println(s); } static void example4() { String[] myArray = new String[]{"bob", "alice", "paul", "ellie"}; String[] myNewArray = Arrays.stream(myArray) .filter(s -> s.length() > 4) .toArray(String[]::new); // Print the contents of the new array for(String s:myNewArray) System.out.println(s); } static void example5() { int myArray[] = { 1, 5, 8 }; int sum = Arrays.stream(myArray).sum(); // Print the sum System.out.println(sum); } static void example6() { String[] myArray = { "this", "is", "a", "sentence" }; String result = Arrays.stream(myArray) .reduce("", (a,b) -> a + b); // Print the result System.out.println(result); } }