Example usage for java.util List parallelStream

List of usage examples for java.util List parallelStream

Introduction

In this page you can find the example usage for java.util List parallelStream.

Prototype

default Stream<E> parallelStream() 

Source Link

Document

Returns a possibly parallel Stream with this collection as its source.

Usage

From source file:Main.java

public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    int sum = numbers.parallelStream().filter(n -> n % 2 == 1).map(n -> n * n).reduce(0, Integer::sum);

    System.out.println(sum);/*w  w  w . ja v a 2s.  c om*/
}

From source file:Main.java

public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

    double sum = numbers.parallelStream().reduce(0.0, (partialSum, a) -> partialSum + a, Double::sum);
    System.out.println(sum);/*from w  ww.  ja v  a 2 s. c om*/

}

From source file:Main.java

public static void main(String[] args) {
    List<String> liste = Arrays.asList("CSS", "HTML", "Davut", null);

    liste.parallelStream()// Parallel stuff uses Fork-Join
            .filter(e -> (!Objects.equals(e, null))) // lazy & parallel
            .filter(e -> (e.length() > 3)) // lazy & parallel
            .forEach(e -> { // sequential & eagerly
                System.out.println("Bigger length than 3 in List: " + e);
            });//from   w ww .  java 2  s  . co  m
}

From source file:Main.java

public static void main(String[] args) {
    List<String> liste = Arrays.asList("XML", "HTML", "CSS", null);

    liste.parallelStream()// Parallel stuff uses Fork-Join
            .filter(e -> (!Objects.equals(e, null))) // lazy & parallel
            .filter(e -> (e.length() > 3)) // lazy & parallel
            .forEach(e -> { // sequential & eagerly
                System.out.println("Bigger length than 3 in List: " + e);
            });//from   w ww .  j  a v a 2  s  .c  o m

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<String> strings = Arrays.asList("a1", "a2", "b1", "c2", "c1");

    strings.parallelStream().filter(s -> {
        System.out.format("filter:  %s [%s]\n", s, Thread.currentThread().getName());
        return true;
    }).map(s -> {//from  w  w  w.  jav  a2 s  .c om
        System.out.format("map:     %s [%s]\n", s, Thread.currentThread().getName());
        return s.toUpperCase();
    }).forEach(s -> System.out.format("forEach: %s [%s]\n", s, Thread.currentThread().getName()));

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<String> strings = Arrays.asList("a1", "a2", "b1", "c2", "c1");

    strings.parallelStream().filter(s -> {
        System.out.format("filter:  %s [%s]\n", s, Thread.currentThread().getName());
        return true;
    }).map(s -> {// w  ww  . j  ava  2s  . c om
        System.out.format("map:     %s [%s]\n", s, Thread.currentThread().getName());
        return s.toUpperCase();
    }).sorted((s1, s2) -> {
        System.out.format("sort:    %s <> %s [%s]\n", s1, s2, Thread.currentThread().getName());
        return s1.compareTo(s2);
    }).forEach(s -> System.out.format("forEach: %s [%s]\n", s, Thread.currentThread().getName()));
}

From source file:Main.java

public static void main(String[] args) {
    List<String> terms = Arrays.asList("this", "is", "is", "a");

    Map<String, Integer> result = terms.parallelStream().flatMap(s -> Arrays.asList(s.split(" ")).stream())
            .collect(Collectors.toConcurrentMap(w -> w.toLowerCase(), w -> 1, Integer::sum));
    System.out.println(result);//from   ww  w. ja  va 2  s.  c o  m
}

From source file:Main.java

public static void main(String[] args) {

    List<Integer> l = Arrays.asList(4, 3, 5, 7, 2, 2, 3, 5);

    l.stream().reduce((a, b) -> a + b).ifPresent(System.out::println);

    l.parallelStream().reduce((a, b) -> a + b).ifPresent(System.out::println);

    System.out.println("isParallel list.stream(): " + l.stream().isParallel());

    System.out.println("isParallel list.stream().parallel(): " + l.stream().parallel().isParallel());

    System.out.println("isParallel list.parallelStream(): " + l.parallelStream().isParallel());

    System.out.println(/*  w  w w . j  a  v  a  2 s  .c  o  m*/
            "isParallel list.parallelStream().sequential(): " + l.parallelStream().sequential().isParallel());
}

From source file:Main.java

public static void main(String[] args) {
    List<Person> roster = createRoster();
    double average = roster.parallelStream().filter(p -> p.getGender() == Person.Sex.MALE)
            .mapToInt(Person::getAge).average().getAsDouble();

    System.out.println("Average age of male members in parallel: " + average);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Person> persons = Arrays.asList(new Person("Max", 18), new Person("Peter", 23),
            new Person("Pamela", 23), new Person("David", 12));

    Integer ageSum = persons.parallelStream().reduce(0, (sum, p) -> {
        System.out.format("accumulator: sum=%s; person=%s\n", sum, p);
        return sum += p.age;
    }, (sum1, sum2) -> {//from  w ww.  j ava2  s  .  co  m
        System.out.format("combiner: sum1=%s; sum2=%s\n", sum1, sum2);
        return sum1 + sum2;
    });

    System.out.println(ageSum);
}