
        Contents                    
                hide
            
            
Introduction
A stream is like a pipeline with start, intermediate, and terminal operations, although it is similar to a Collection, however, there are some differences
- Stream does not store data
 - Stream does not change the data source
 - Stream is by default lazy
 
Write 5 Stream Sources
- Stream.of(“red”, “blue”, “green”, “black”);
 - Stream.generate(Math::random);
 - list.stream()
 - Arrays.stream(array)
 - IntStream.range(1, 3);
 
Convert a List of Strings(India, Canada, USA) to a List Of Characters
public class FlatMap {
    public static void main(String[] args) {
        List<String> words = Arrays.asList(new String[]{"India", "Canada", "USA"});
        Stream<Stream<Character>> result1 = words.stream().map(w -> characterStream(w));
        result1.forEach(System.out::println);
        Stream<Character> result2 = words.stream().flatMap(w -> characterStream(w));
        result2.forEach(System.out::println);
    }
    public static Stream<Character> characterStream(String s) {
        List<Character> result = new ArrayList<>();
        for (char c : s.toCharArray()) result.add(c);
        return result.stream();
    }
}
Finding the largest Element without using Built In Function from 4,8,24,3,22
    private static void reduce() {
        Optional<Integer> optional = Stream.of(4,8,24,3,22).reduce((i1, i2) -> i1 > i2 ? i1: i2);
        System.out.println(optional.get());
    }
Find the sum of the Words length – India, Canada, USA
    private static void combinerAndAccumulator() {
        Stream<String> strings = Stream.of("India", "Canada", "USA");
        int result = strings.reduce(0, (x , y) -> x + y.length(), (x, y) -> x + y);
        System.out.println(" Total SUM  " + result);
    }
Partition the List of Employees by Salary > x, Employee = id, name, salary
    private static void partitionByDemo() {
        Employee e1 = new Employee(1, "Jones", 1000);
        Employee e2 = new Employee(2, "Smith", 1200);
        Employee e3 = new Employee(3, "Adam", 1000);
        Employee e4 = new Employee(4, "Chris", 1100);
        Employee e5 = new Employee(5, "Dave", 1200);
        List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5);
        Map<Boolean, List<Employee>> employees1 = employees.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 1000));
        System.out.println(employees1);
    }
Group a List of Employees by Salary and name -> Employee = id, name, salary
public class Grouping {
    public static void main(String[] args) {
        groupingByDemo();
    }
    private static void groupingByDemo() {
        Employee e1 = new Employee(1, "Jones", 1000);
        Employee e2 = new Employee(2, "Smith", 1200);
        Employee e3 = new Employee(3, "Adam", 1000);
        Employee e4 = new Employee(4, "Chris", 1100);
        Employee e5 = new Employee(5, "Dave", 1200);
        List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5);
        Map<Integer, List<String>> groupBySalary = employees.stream()
                .collect(Collectors.groupingBy(x -> x.salary, Collectors.mapping(Employee::getName, Collectors.toList() )));
        System.out.println(groupBySalary);
    }
}