Example usage for java.util List stream

List of usage examples for java.util List stream

Introduction

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

Prototype

default Stream<E> stream() 

Source Link

Document

Returns a sequential 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);
    numbers.stream().map(n -> n * n).forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    numbers.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
}

From source file:Main.java

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

    numberList.stream().map(n -> n * 2).forEach(System.out::println);
}

From source file:Main.java

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

    stringList.stream().forEachOrdered(System.out::println);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    // map/*  ww w.  j a  va2  s  .  com*/
    List<String> words = Arrays.asList("Hello", "World");

    words.stream().flatMap((String line) -> Arrays.stream(line.split(""))).distinct()
            .forEach(System.out::println);

}

From source file:Main.java

public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    int sum = numbers.stream().reduce(0, Integer::sum);
    System.out.println(sum);/*from   w  ww .j  a va2 s . c  o  m*/
}

From source file:Main.java

public static void main(String... args) {
    List<Integer> numbers = Arrays.asList(3, 4, 5, 1, 2);
    int sum = numbers.stream().reduce(0, (a, b) -> a + b);
    System.out.println(sum);//from   w  ww.  ja  va2  s  . c om
}

From source file:Main.java

public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    int i = numbers.stream().filter(n -> n % 2 == 0).findFirst().orElse(-1);

    System.out.println(i);/*w ww.  j  a v a2s  .c om*/
}

From source file:Main.java

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

    System.out.println(o);/*from  ww  w  . j  a  va  2  s . co m*/
}

From source file:Main.java

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

    System.out.println(sum);//from ww w .j  a v  a 2  s. co m
}