Working with Primitive Arrays
In the previous steps, we worked with arrays of reference types (String). However, Java arrays can also contain primitive types such as int
, double
, etc. Converting primitive arrays to Lists requires additional steps because Java generics only work with reference types.
Let's create a new example to demonstrate this process.
Create a New Java File
-
Create a new Java file called PrimitiveArrayToList.java
in the same directory:
From the WebIDE, navigate to project/src/main/java
, right-click, and select "New File". Name it PrimitiveArrayToList.java
.
-
Add the following code to the file:
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class PrimitiveArrayToList {
public static void main(String[] args) {
System.out.println("Converting Primitive Arrays to Lists");
System.out.println("===================================");
// Create a primitive int array
int[] numbersArray = {10, 20, 30, 40, 50};
System.out.println("Original primitive array: " + Arrays.toString(numbersArray));
// Method 1: Manual conversion
System.out.println("\nMethod 1: Manual conversion");
List<Integer> numbersList1 = new ArrayList<>();
for (int number : numbersArray) {
numbersList1.add(number); // Autoboxing converts int to Integer
}
System.out.println("List after manual conversion: " + numbersList1);
// Method 2: Using Java 8 Streams
System.out.println("\nMethod 2: Using Java 8 Streams");
List<Integer> numbersList2 = Arrays.stream(numbersArray)
.boxed() // Converts IntStream to Stream<Integer>
.collect(Collectors.toList());
System.out.println("List after stream conversion: " + numbersList2);
// Modify the lists to demonstrate independence from the array
System.out.println("\nModifying the lists:");
numbersList1.add(60);
numbersList1.set(0, 15);
numbersList2.add(70);
numbersList2.remove(0);
System.out.println("List 1 after modifications: " + numbersList1);
System.out.println("List 2 after modifications: " + numbersList2);
System.out.println("Original array after List modifications: " + Arrays.toString(numbersArray));
// Create and convert other primitive type arrays
System.out.println("\nOther primitive type examples:");
double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5};
List<Double> doubleList = Arrays.stream(doubleArray)
.boxed()
.collect(Collectors.toList());
System.out.println("Double array: " + Arrays.toString(doubleArray));
System.out.println("Double list: " + doubleList);
boolean[] boolArray = {true, false, true, true, false};
List<Boolean> boolList = new ArrayList<>();
for (boolean value : boolArray) {
boolList.add(value);
}
System.out.println("Boolean array: " + Arrays.toString(boolArray));
System.out.println("Boolean list: " + boolList);
}
}
- Compile and run the program:
cd ~/project
javac src/main/java/PrimitiveArrayToList.java
java -cp src/main/java PrimitiveArrayToList
You should see output similar to this:
Converting Primitive Arrays to Lists
===================================
Original primitive array: [10, 20, 30, 40, 50]
Method 1: Manual conversion
List after manual conversion: [10, 20, 30, 40, 50]
Method 2: Using Java 8 Streams
List after stream conversion: [10, 20, 30, 40, 50]
Modifying the lists:
List 1 after modifications: [15, 20, 30, 40, 50, 60]
List 2 after modifications: [20, 30, 40, 50, 70]
Original array after List modifications: [10, 20, 30, 40, 50]
Other primitive type examples:
Double array: [1.1, 2.2, 3.3, 4.4, 5.5]
Double list: [1.1, 2.2, 3.3, 4.4, 5.5]
Boolean array: [true, false, true, true, false]
Boolean list: [true, false, true, true, false]
Understanding Primitive Array Conversion
When working with primitive arrays, there are two key considerations:
-
Autoboxing: Java automatically converts primitive values to their wrapper class objects when adding to collections. For example, int
is converted to Integer
.
-
Boxing Methods for Streams: When using streams with primitive arrays, you need to call the .boxed()
method to convert primitive streams to object streams.
The conversion process creates completely new Lists that are independent of the original arrays. This means:
- Modifying the List will not affect the original array
- The Lists are fully mutable (you can add, remove, or change elements)
- Each element in the List is a new object (wrapper) that contains the value from the array
This independence is particularly useful when you need to manipulate the data without risking changes to the original array.