Transforming Intermediate Stream Types in Java: IntStream, Stream<Integer>, OptionalInt, and Optional<Integer>

IntStream and Stream&lt;Integer&gt; often appear as intermediate phases when working with numeric data in the Java Stream API. Terminal operations like sum, average, min, or max typically yield OptionalInt or Optional&lt;Integer&gt; to safely handle potentially missing values. Understanding how to create these streams, convert between them, and map them to optionals is essantial for writing fluent data pipelines.

Constructing IntStream and Stream<Integer>

Both stream types can be instantiated from collections or arrays. The underlying element type dictates wich stream variant is produced.

import java.util.*; 
import java.util.stream.*;

public class StreamConstruction {
    public static void main(String[] args) {
        List&lt;Integer&gt; itemList = Arrays.asList(10, 20, 30, 40, 50);
        Integer[] boxedArr = {10, 20, 30, 40, 50};
        int[] primitiveArr = {10, 20, 30, 40, 50};

        Stream&lt;Integer&gt; fromList = itemList.stream();
        Stream&lt;Integer&gt; fromBoxedArray = Arrays.stream(boxedArr);
        IntStream fromPrimitiveArray = Arrays.stream(primitiveArr);

        fromList.forEach(val -> System.out.print(val + " "));
        System.out.println();
        fromBoxedArray.forEach(val -> System.out.print(val + " "));
        System.out.println();
        fromPrimitiveArray.forEach(val -> System.out.print(val + " "));
    }
}

Converting Between IntStream and Stream<Integer>

Switching between the primitive-specialized IntStream and the boxed Stream&lt;Integer&gt; is straightforward with dedicated intermediary methods.

  • IntStream → Stream<Integer>: Invoke boxed().
  • Stream<Integer> → IntStream: Apply mapToInt(Integer::intValue).
import java.util.*; 
import java.util.stream.*;

public class StreamConversion {
    public static void main(String[] args) {
        List&lt;Integer&gt; original = Arrays.asList(2, 4, 6, 8, 10);
        List&lt;Integer&gt; roundtripped = original.stream()
                .mapToInt(Integer::intValue)
                .boxed()
                .collect(Collectors.toList());
        System.out.println(roundtripped);    // [2, 4, 6, 8, 10]
    }
}

Streaming Into Optional Types

Terminal reduction operations on numeric streams naturally return optionals to avoid nullability. The output type depends on whether the stream processes boxed or primitive values.

import java.util.*;
import java.util.stream.*;

public class StreamToOptional {
    public static void main(String[] args) {
        List&lt;Integer&gt; numbers = Arrays.asList(7, 2, 9, 4, 6);

        OptionalInt maxPrimitive = numbers.stream()
                .mapToInt(Integer::intValue)
                .max();
        System.out.println(maxPrimitive.getAsInt());   // 9

        Optional&lt;Integer&gt; maxBoxed = numbers.stream()
                .max(Integer::compareTo);
        System.out.println(maxBoxed.get());            // 9
    }
}

Interconverting int[], Integer[], and List<Integer>

Combining array and collection transformations with boxed/unboxed streams allows full flexibility when working with numeric data.

int[] → Integer[]

int[] srcPrimitive = {3, 5, 7};
Integer[] destBoxed = Arrays.stream(srcPrimitive)
        .boxed()
        .toArray(Integer[]::new);
System.out.println(Arrays.toString(destBoxed));   // [3, 5, 7]

Integer[] → int[]

Integer[] srcBoxed = {4, 6, 8};
int[] destPrimitive = Arrays.stream(srcBoxed)
        .mapToInt(Integer::intValue)
        .toArray();
System.out.println(Arrays.toString(destPrimitive));   // [4, 6, 8]

int[] → List<Integer>

int[] srcPrimitive = {2, 4, 6};
List&lt;Integer&gt; resultList = Arrays.stream(srcPrimitive)
        .boxed()
        .collect(Collectors.toList());
System.out.println(resultList);                       // [2, 4, 6]

List<Integer> → int[]

List&lt;Integer&gt; srcList = Arrays.asList(9, 7, 5);
int[] resultPrimitive = srcList.stream()
        .mapToInt(Integer::intValue)
        .toArray();
System.out.println(Arrays.toString(resultPrimitive)); // [9, 7, 5]

Integer[] → List<Integer>

Integer[] srcBoxed = {1, 3, 5};
List&lt;Integer&gt; resultList = Arrays.stream(srcBoxed)
        .collect(Collectors.toList());
System.out.println(resultList);                       // [1, 3, 5]

List<Integer> → Integer[]

List&lt;Integer&gt; srcList = Arrays.asList(8, 6, 4);
Integer[] resultBoxed = srcList.stream()
        .toArray(Integer[]::new);
System.out.println(Arrays.toString(resultBoxed));     // [8, 6, 4]

Tags: java Stream API IntStream OptionalInt Code Examples

Posted on Mon, 15 Jun 2026 16:25:22 +0000 by Janjan