Best way to convert Primitive Array to List in Java8 and Classic toString(), Iterator way

Java Stream() is an amazing util class with lots of functionalities. We have published almost ~30+ Java8 Stream related tutorial on Crunchify and this one is on stream().boxed.collect() utility.

We will convert Primitive Array to List in 3 different ways.

  1. Java8 Stream() way
  2. Classic Java toString() way
  3. Class Java Iterator way – using for loop

Before we get started let’s take a look at below utils:

  • stream() returns a sequential IntStream with the specified array as its source.
  • boxed() returns a Stream consisting of the elements of this stream, each boxed to an Integer.
  • collect() performs a mutable reduction operation on the elements of this stream using a Collector.

Let’s get started.

Step-1

We are going to perform below tasks:

  • Create class PrimitiveArrayToListJava8.java
  • Create main() method
  • Inside main() method, we will add 15 random integers and adds to variable crunchifyValue int[] array which is primitive array.
  • Using Java8 Stream() class we will convert that primitive array to List
  • Create another public method crunchifyClassWaytoConvert(int[] crunchifyValue) which we will use to convert primitive array to list using Pre-Java8 (Classic way) using For loop and toString() methods

Same way you could also convert:

  • Convert an array of primitive longs into a List of Longs
  • How to convert int[] into List<Integer> in Java?
  • Convert between primitive array, boxed array, and List

Step-2

Open Eclipse IDE and create class PrimitiveArrayToListJava8.java and put below code.

package crunchify.com.tutorials;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Crunchify.com 
 * version: 1.2
 * 
 */

public class PrimitiveArrayToListJava8 {

        public static void main(String[] args) {
                int randomNumber;

                // add random value to integer array

                int[] crunchifyValue = new int[15]; // we are adding total 15 primitive values to int[] array.
                for (int i = 1; i <= 14; i++) {
                        randomNumber = (5 + (int) (Math.random() * ((100 - 5))));
                        crunchifyValue[i] = randomNumber;
                }

                // Java8 - Stream way to convert Primitive array to List.
                // stream() returns a sequential IntStream with the specified array as its source.
                // Boxed() returns a Stream consisting of the elements of this stream, each boxed to an Integer.
                // collect() performs a mutable reduction operation on the elements of this stream using a Collector.
                List<Integer> crunchifyCovertedList = Arrays.stream(crunchifyValue).boxed().collect(Collectors.toList());
                System.out.println("Java8 way: Using stream().boxed.collect() : " + crunchifyCovertedList);

                // Classic way to convert Primitive array to List
                crunchifyClassWaytoConvert(crunchifyValue);
        }

        public static void crunchifyClassWaytoConvert(int[] crunchifyValue) {

                List<Integer> crunchifyIntList = new ArrayList<Integer>();

                System.out.println("\nClassic way 1: Print crunchifyIntList Value via toString(): " + Arrays.toString(crunchifyValue));

                for (int i : crunchifyValue) {
                        crunchifyIntList.add(i);
                }

                // if you want to use Iterator to print value
                System.out.println("\nClassic way 2: Print crunchifyIntList Value via Iterator: " + crunchifyIntList);

        }
}

Step-3

Run program in Eclipse and checkout below result in Eclipse Console Output.

Java8 way: Using stream().boxed.collect() : [0, 16, 90, 85, 58, 71, 42, 87, 40, 59, 78, 88, 77, 98, 44]

Classic way 1: Print crunchifyIntList Value via toString(): [0, 16, 90, 85, 58, 71, 42, 87, 40, 59, 78, 88, 77, 98, 44]

Classic way 2: Print crunchifyIntList Value via Iterator: [0, 16, 90, 85, 58, 71, 42, 87, 40, 59, 78, 88, 77, 98, 44]

The post Best way to convert Primitive Array to List in Java and Classic toString(), Iterator way appeared first on Crunchify.