Java Stream Examples
### Problem #1: Given an integer array sorted in increasing order, return an array of the squares of each number sorted in increasing order. ### Solution: ``` public static int[] sortedSquaresArray(int[] numbers) { return Arrays.stream(numbers).boxed() .map(val -> val * val) .sorted() .mapToInt(value -> value.intValue()) .toArray(); } ``` If you want to return the squares of each number sorted in descending order then you can use *.sorted(Collections.reverseOrder())* ### Problem #2: Given ArrayList of Employee (name, age) object. Print the names of employees having age greater than 20 saperated with comma. ### Solution: ``` String str = employeeList.stream() .filter(e -> e.age >20) .map(e -> e.name) .collect(Collectors.joining(",")); System.out.println(str); ``` ### Problem#3: Create a reverse map...