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());

}

 

 

 

 

 

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>> groupedMap = people.stream()

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

        System.out.println(groupedMap.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);

    }

   

}

 

 

 

 

 

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);

    }

}