//SomeStreamExamples.java
//Based on some examples from this website:
//https://www.tutorialspoint.com/java8/java8_streams.htm

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class SomeStreamExamples
{
    public static void main(String[] args)
    {
        System.out.println("=====1==============================");
        //Output just ten random integers, sorted, one per line:
        new Random()
        .ints(20, 25)
        .limit(10)
        .sorted()
        .forEach(System.out::println);
        System.out.println();

        Random randGen = new Random();
        randGen.setSeed(100); //Will give same values each time
        randGen
        .ints(20, 25)
        .limit(10)
        .sorted()
        .forEach(System.out::println);

        System.out.println("=====2==============================");
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        //You can generate and save a list of unique squares:
        List<Integer> squaresList = numbers
                                    .stream()
                                    .map(i -> i * i)
                                    .distinct()
                                    .collect(Collectors.toList());

        //But, to display them do this ...
        numbers
        .stream()
        .map(i -> i * i)
        .distinct()
        .collect(Collectors.toList())
        .forEach(System.out::println);
        //and note that we can't just tack on the forEach()
        //to the preceding code segment because it's void
        //and in that segment we're assigning the list.

        System.out.println("=====3==============================");
        List<String> strings
            = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        //Get the count of empty strings:
        long count = strings //Note: count must be "long", not "int"
                     .stream()
                     .filter(string -> string.isEmpty())
                     .count();
        System.out.println(count);

        //Display all non-empty strings:
        strings
        .stream()
        .filter(string -> !string.isEmpty())
        .collect(Collectors.toList())
        .forEach(System.out::println);

        System.out.println("=====4==============================");
        //Filter out the non-empty strings, then combine them into
        //a single string in which the original strings are separated
        //by a comma followed by a blank space:
        List<String> filtered = strings
                                .stream()
                                .filter(string -> !string.isEmpty())
                                .collect(Collectors.toList());
        System.out.println("Filtered List: " + filtered);
        String mergedString = strings
                              .stream()
                              .filter(string -> !string.isEmpty())
                              .collect(Collectors.joining(", "));
        System.out.println("Merged String: " + mergedString);

        System.out.println("=====5==============================");
        //Compute some "statistics" on a list of integers:
        List<Integer> integers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        IntSummaryStatistics stats = integers
                                     .stream()
                                     .mapToInt(x -> x)
                                     .summaryStatistics();
        System.out.println("Highest number in List...... "
                           + stats.getMax());
        System.out.println("Lowest number in List....... "
                           + stats.getMin());
        System.out.println("Sum of numbers in List...... "
                           + stats.getSum());
        System.out.println("Average of numbers in List.. "
                           + stats.getAverage());
    }
}
/*  Output:
    =====1==============================
    20
    20
    21
    22
    23
    23
    23
    24
    24
    24

    20
    20
    21
    21
    21
    23
    23
    23
    23
    24
    =====2==============================
    9
    4
    49
    25
    =====3==============================
    2
    abc
    bc
    efg
    abcd
    jkl
    =====4==============================
    Filtered List: [abc, bc, efg, abcd, jkl]
    Merged String: abc, bc, efg, abcd, jkl
    =====5==============================
    Highest number in List...... 7
    Lowest number in List....... 2
    Sum of numbers in List...... 25
    Average of numbers in List.. 3.5714285714285716
*/
