Pages

Monday, December 16, 2019

Java 8 Program To Boxed Stream Operations

1. Overview


In this tutorial, We'll learn how to convert primitives to wrapper objects in java 8. Java programs to convert a stream of int, long, double values into Collection using boxed() method.

This boxed() method is present in IntStream, LongStream, and DoubleStream primitive specialization streams.

boxed() method returns a Stream consisting of the elements of this stream, each boxed to a Wrapper.


Java 8 Program To Boxed Stream Operations




Stream of Strings:


Basically, converting String values into List works very well without any errors.

package com.javaprogramto.w3schools.programs.java8.boxed;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Java8Boxed {

    public static void main(String[] args) {

        // Creating String Stream
        Stream stringStream = Stream.of("java", "program", "to", ".com");

        // converting String's Stream to List
        List strList = stringStream.collect(Collectors.toList());

        // printing each value from list.
        System.out.println("List values are : ");
        strList.forEach(value -> System.out.println(value));

    }
}

Stream of int values into List:


        // Creating String Stream
        IntStream intStream = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);

        // converting String's Stream to List
        intStream.collect(Collectors.toList());


This code will end up in a compile-time error.

The solution is boxed() method for the primitive streams.

2. IntStream.boxed() method Example


Converting primitive int to Wrapper Integer using boxed() method.

Example program to convert int stream to List of Integers:

package com.javaprogramto.w3schools.programs.java8.boxed;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * Java Program To IntStream.boxed() method
 * 
 * @author JavaProgramTo.com
 *
 */
public class IntStreamBoxedExample {

    public static void main(String[] args) {

        // creating int stream
        IntStream intStream = IntStream.of(1, 2, 3, 4, 5);
        
        // using boxed() method for boxing primitive int.
        List intList = intStream.boxed().collect(Collectors.toList());

        // print list
        intList.forEach(value -> System.out.println(value));

    }

}

Output:

1
2
3
4
5


3. LongStream.boxed() method Example


Converting primitive long to Wrapper Long using boxed() method.

Example program to convert the long stream to List of Long values:

package com.javaprogramto.w3schools.programs.java8.boxed;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

/**
 * Java Program To LongStream.boxed() method
 * 
 * @author JavaProgramTo.com
 *
 */
public class LongStreamBoxedExample {

    public static void main(String[] args) {

        // creating long primitive stream
        LongStream longStream = LongStream.of(1l, 2l, 3l, 4l, 5l);

        // using boxed() method for boxing primitive long.
        List longList = longStream.boxed().collect(Collectors.toList());

        // print list
        longList.forEach(value -> System.out.println(value));

    }

}

Output:

1
2
3
4
5

4. DoubleStream.boxed() method Example


Converting primitive double to Wrapper Double using boxed() method.

Example program to convert the double stream to List of Double:

package com.javaprogramto.w3schools.programs.java8.boxed;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;

/**
 * Java Program To DoubleStream.boxed() method
 * 
 * @author JavaProgramTo.com
 *
 */
public class DoubleStreamBoxedExample {

    public static void main(String[] args) {

        // creating double primitive stream
        DoubleStream doubleStream = DoubleStream.of(1d, 2d, 3d, 4d, 5d);

        // using boxed() method for boxing primitive long.
        List doubleList = doubleStream.boxed().collect(Collectors.toList());

        // print list
        doubleList.forEach(value -> System.out.println(value));

    }

}

Output:


1.0
2.0
3.0
4.0
5.0

5. Conclusion


In this article, We've seen how to convert primitive int, long and double streams to List of Integer, Double and Long wrapper values. Shown the examples using IntStream, DoubleStream and LongStream boxed() method.

No comments:

Post a Comment

Please do not add any spam links in the comments section.