package streams;

 

import java.util.Arrays;

import java.util.List;

 

public class EvenOnly {

    public static void main(String[] args) {

        List<Integer> l = Arrays.asList(5,4,88,76,55,23,7,20,45);

        l.stream().filter(p -> p%2==1)

.forEach(i -> System.out.print(i+" "));

    }

}

 

 

 

public static void main(String[] args) {

        List<Integer> l = Arrays.asList(5,4,88,76,55,23,7,20,45);

        Stream<Integer> s = l.stream().filter(p -> {

            System.out.println("Filter called on "+ p);

            return p%2==1;

        });

        System.out.println( "The pipeline is created - calling terminal operation");

        s.forEach(i -> System.out.println(">>>" + i));

}

 

 

 

public static void main(String[] args) {

        List<Integer> l = Arrays.asList(5,4,88,76,55,23,7,20,45);

        Stream<Integer> s = l.stream().filter(p -> {

            System.out.println("Filter called on "+ p);

            return p%2==1;

        });

  Optional<Integer> result = s.findFirst();

        System.out.println(result.get());

}

 

 

 

double result = l.stream()

.filter(p -> p%2==1)

.mapToInt(p -> p * p)

.sum()

 

 

 

l.stream()

.filter(p -> p%2==1)

.mapToInt(p -> p * p)

.reduce((a, b) -> a + b)

 

 

 

l.stream()

.filter(p -> p%2==1)

.mapToInt(p -> p * p)

.reduce(Integer::sum)

 

 

l.stream()

.filter(p -> p%2==1)

.mapToInt(p -> p * p)

.reduce(100, (a, b) -> a + b)

 

 

 

package streams;

 

import java.util.Arrays;

import java.util.List;

import java.util.Map;

import java.util.stream.Collectors;

 

public class Collecting {

 

    public static void main(String[] args) {

        List<Person> people = Arrays.asList(

                new Person("Albert", "Smith"),

                new Person("John", "James"),

                new Person("John", "Brown"),

                new Person("William", "Hughes"),

                new Person("William", "Fleming")

        );

       

        Map<String, List<Person>> groupedList = people.stream()

            .collect(Collectors.groupingBy(Person::getFirstName));

        System.out.println(groupedList.get("William"));

    }

   

}

 

 

package streams;

import java.util.Arrays;

import java.util.Map;

import java.util.TimeZone;

import java.util.stream.Collectors;

 

public class Collecting5 {

    public static void main(String[] args) {

        Map<String, Long> result =

                Arrays.asList(TimeZone.getAvailableIDs())

                .stream()

                .filter(s -> s.indexOf("/") > 0)

                .collect(Collectors.groupingBy((String s) -> s.substring(0, s.indexOf("/")),

                        Collectors.counting()));

        System.out.println(result);

    }

}

 

 

package streams;

import java.util.Arrays;

import java.util.List;

import java.util.Map;

import java.util.TimeZone;

import java.util.stream.Collectors;

 

public class Collecting4 {

    public static void main(String[] args) {

        List<String> zones = Arrays.asList(TimeZone.getAvailableIDs());

       

        Map<Boolean, List<String>> zonesCompare =

         zones.stream()

                 .collect(Collectors.partitioningBy(s -> s.indexOf("/") > 0));

        System.out.println(zonesCompare);

    }

}

 

 

package streams;

 

import java.util.Arrays;

import java.util.List;

import java.util.stream.Collectors;

 

public class Collecting {

 

    public static void main(String[] args) {

        List<String> strings = Arrays.asList("This", "is", "a", "list");

        String s = strings.stream()

            .collect(Collectors.joining(", "));

        System.out.println(s);

    }

}

 

 

package streams;

 

import java.util.Arrays;

import java.util.List;

import java.util.Map;

import java.util.stream.Collectors;

 

public class Collecting {

 

    public static void main(String[] args) {

        List<Person> people = Arrays.asList(

                new Person("Albert", "Smith"),

                new Person("John", "James"),

                new Person("John", "Brown"),

                new Person("William", "Hughes"),

                new Person("William", "Fleming")

        );

       

        List<Person> filteredList = people.stream()

            .filter(p -> p.getFirstName().equals("William"))

            .collect(Collectors.toList());

        System.out.println(filteredList);

    }

   

}

 

 

IntStream.range(0, 10).forEach(n -> System.out.println(n));

 

 

 

Stream.builder().add("Hello").add("World").add("!").build()

.forEach(s -> System.out.print(s));

 

 

 

Stream s1 = Stream.builder().add( "Hello").add("World").add("!").build();

Stream s2 = Stream.builder().add( "Another").add("Stream").build();      

Stream.concat(s1, s2).forEach(s -> System.out.print(s));

 

 

 

package streams;

 

import java.io.IOException;

import java.nio.file.*;

import java.util.logging.*;

 

public class FileReading {

    public static void main(String[] args) {

        Path path = Paths.get("/tmp/test.txt");

        try {

            Files.lines(path).distinct()

.forEach(s -> System.out.println(s));

        } catch (IOException ex) {

            Logger.getLogger(FileReading.class.getName())

.log(Level.SEVERE, "IO Error", ex);

        }

    }

}

 

 

 

package streams;

 

import java.util.Arrays;

 

public class ArraysStream {

    public static void main(String[] args) {

        int[] intArray = {4,2,5,7,66,4,3};

        int result = Arrays.stream(intArray).sum();

        System.out.print(result);

    }

}

 

 

 

l.parallelStream().filter(p -> p%2==1).mapToInt(p -> p * p).sum()

 

 

 

package streams;

 

import java.util.OptionalDouble;

import java.util.stream.IntStream;

 

public class LargeRange {

    public static void main(String[] args) {

        long start = System.currentTimeMillis();

        OptionalDouble result = IntStream.range(0, Integer.MAX_VALUE)

.parallel()

.filter(i -> i % 2 == 0)

.average();

        System.out.printf("Parallel version took %d\n",

 System.currentTimeMillis()-start);

        start = System.currentTimeMillis();

        OptionalDouble result2 = IntStream.range(0, Integer.MAX_VALUE)

.filter(i -> i % 2 == 0)

.average();

        System.out.printf("Serial version took %d\n",

 System.currentTimeMillis()-start);

    }

}

 

 

 

package streams;

 

import java.util.OptionalInt;

import java.util.stream.IntStream;

 

public class Associative {

 

    public static void main(String[] args) {

        OptionalInt result =  IntStream.range(1, 1000)

             .parallel()

             .reduce((a, b) -> a-b);

        System.out.println(result.getAsInt());

        result =  IntStream.range(1, 1000)

              .reduce((a, b) -> a-b);

        System.out.println(result.getAsInt());

    }

}

 

 

 

IntStream.range(1, 1000).reduce((a, b) -> a+b);

 

 

 

package streams;

 

import java.util.stream.IntStream;

 

public class Associative2 {

 

    public static void main(String[] args) {

        System.out.println("Running in parallel");

        IntStream.range(1, 1000)

          .parallel()

          .filter(a -> a %2 == 1)

          .limit(10)

          .sorted()

         .forEach(a -> System.out.println(a));

        System.out.println("Running in serial");

        IntStream.range(1, 1000)

         .filter(a -> a %2 == 1)

         .limit(10)

         .forEach(a -> System.out.println(a));

    }

}

 

 

 

IntStream.range(1, 1000).parallel()

.filter(a -> a %2 == 1)

.limit(10)

.forEachOrdered(a -> System.out.println(a));