Java Streams - IntStream collect(Supplier supplier, ObjIntConsumer accumulator, BiConsumer combiner) example








IntStream collect(Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R,R> combiner) performs a mutable reduction operation on the elements of this stream.

Syntax

collect has the following syntax.

<R> R collect(Supplier<R> supplier,  ObjIntConsumer<R> accumulator,  BiConsumer<R,R> combiner)

Example

The following example shows how to use collect.

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
//from w w  w .  j  a  va 2s. c  o  m
public class Main {
  public static void main(String[] args) {
    IntStream i = IntStream.of(6,5,7,1, 2, 3, 3);
    List<Integer> v = i
        .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
    
    System.out.println(v);
  }
}

The code above generates the following result.