Java Boxed Stream Example

Java a boxed stream is a stream of the wrapper class instances to simulate a stream of primitives.

1. What is a Boxed Stream?

In Java 8, if we want to convert a stream of objects to a collection then we can use one of the static methods in the Collectors class.

//It works perfectly!!
List<String> strings = Stream.of("how", "to", "do", "in", "java")
    				.collect(Collectors.toList());

However, the same process doesn’t work on streams of primitives.

//Compilation Error !!
IntStream.of(1,2,3,4,5)
    .collect(Collectors.toList());

To convert a stream of primitives, we must first box the elements in their wrapper classes and then collect the wrapped objects in a collection. This type of stream is called boxed stream.

Let’s look at a few examples of boxed streams in Java.

2. IntStream (Stream of int)

Example to convert int stream to List of Integers.

//Get the collection and later convert to stream to process elements
List<Integer> ints = IntStream.of(1,2,3,4,5)
				.boxed()
				.collect(Collectors.toList());
		
System.out.println(ints);

//Stream operations directly
Optional<Integer> max = IntStream.of(1,2,3,4,5)
				.boxed()
				.max(Integer::compareTo);

System.out.println(max);

Program Output:

[1, 2, 3, 4, 5]
5

3. LongStream (Stream of long)

Example to convert long stream to List of Longs.

List<Long> longs = LongStream.of(1l,2l,3l,4l,5l)
				.boxed()
				.collect(Collectors.toList());
		
System.out.println(longs);

Output:

[1, 2, 3, 4, 5]

4. DoubleStream (Stream of double)

Example to convert double stream to List of Doubles.

List<Double> doubles = DoubleStream.of(1d,2d,3d,4d,5d)
				.boxed()
				.collect(Collectors.toList());
		
System.out.println(doubles);

Output:

[1.0, 2.0, 3.0, 4.0, 5.0]

Drop me your questions in comments section related to boxed streams or stream of primitives in Java Stream API.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

3 thoughts on “Java Boxed Stream Example”

  1. int[] answer = new int[] { 3, 2 }
    List<Integer> expected = Arrays.stream(answer).boxed().collect(Collectors.toList());
    
    Object[] objArrayOfIntArrays = new Object[] {
                    new int[] { 1, 2 }, // add 2
                    new int[] { 1, 2 }, // add 2
                    new int[] { 3, 2 }, // query freq 2
                    new int[] { 2, 2 }, // delete 2
                    new int[] { 3, 2 }, // query freq 2
                }
    

    My question is, is there a way to turn objArrayOfIntArrays to List<List<Integer>> ?

    Reply
    • Hi Tim,

       
      Object[] objArrayOfIntArrays1 = new Object[] { new int[] { 1, 2 }, // add 2
      		new int[] { 1, 2 }, // add 2
      		new int[] { 3, 2 }, // query freq 2
      		new int[] { 2, 2 }, // delete 2
      		new int[] { 3, 2 }, // query freq 2
      };
      
      List<List<Integer>> primitiveList = Arrays
      					.stream(objArrayOfIntArrays1)
      					.map(q -> {
      						return Arrays.stream((int[]) q)
      						        .boxed()
      							.collect(Collectors.toList());
      						}).collect(Collectors.toList());
      
      

      Hope it Helps,
      Praveen

      Reply
    •  
       Object[] objArrayOfIntArrays1 = new Object[] {
                   new int[] { 1, 2 }, // add 2
                   new int[] { 1, 2 }, // add 2
                   new int[] { 3, 2 }, // query freq 2
                   new int[] { 2, 2 }, // delete 2
                   new int[] { 3, 2 }, // query freq 2
               };
        	   
        	 List<List<Integer>> primitiveList=Arrays.stream(objArrayOfIntArrays1)
      		       .map(q->{return Arrays.stream((int[]) q).boxed().collect(Collectors.toList());}).collect(Collectors.toList());
      
      Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.