"List<Integer>" vs "Int[]", It Seems Printing The Same. Are Conversion Necessary and How to Convert it.
#Foreword
Before we begin, I want to clarify that I am not a professional programmer, I am just endeavoring to create a document to oragnize my thoguhts for my future reference. This document will focus on conversion using 'stream()', a topic that I initially believed to be straightforward. But I've come to realize that it's a group of several concepts. To be honest, when faced with coding problems, the specific conditions would be predefined, allowing us to convert data type as needed without delving deeply into the underlying mechanism. which means we don't need to be thinking about it deeply. I've analyzed the short code snippets, I've found myself struggling to comprehend the conversionBy analyzing the short code snippets, I've found myself struggling to understand certain concetps. Therefore, I've decided to create a document that delves into these related thoeries.
public class AreConversionNecessary {
public static void main(String[] args) {
// Create a list of Integer Objects.
List<Integer> numbers = Arrays.asList(1,2,3,4,5);
// It's a List, so we can print it out directly
// outcome : numbers = [1,2,3,4,5]
System.out.println("number = " + numbers);
for (Integer number : numbers) {
System.out.println("number = " + number);
}
// using Stream to convert list (numbers) to primitive int Array.
int[] primitiveIntArray = numbers.stream()
.mapToInt(Integer::intValue)
.toArray();
//No Specific value : (printed the Address of Array Object) : [I@4f3f5b24
System.out.println(primitiveIntArray);
// after converting int[], it prints out int[]'s elements.
System.out.println("primitiveIntArray = "
+ Arrays.toString(primitiveIntArray));
}
}
#As you see, the results of console seems same.
: Integer is a wrapper class and object, but int[] is an array of primitive int data type. Theoratically, the memory efficiency of primitive data type is better than the wrapper class (Object) and the performan is better either.
As you see the results from the console, They seems same with each other. If so, are they really same? When it comes to [1,2,3,4,5], even if they seem same, their data structure are both different, List<Integer> and int[]. (List<Integer> : a generic interface in Java's Collection framework // int[] : a basic array in Java that stores primitive int values. )
Although I studied these concepts, I haven't yet to fully understand. (That's why I am making a document.)
** Frankly, The reason why I want to make a document is because of the code below.
#I couldn't understand the code below, I think the best way to understand codes is to analyze every single line.
Given Condition
1. Choose 2 elements from numbers array and then add two numbers.
2. Save the extracted number without duplicates.
3. Sort the array in ascending order and return it.
import java.util.HashSet;
public class AreConversionNecessary2 {
public static int[] solution(int[] numbers) {
// for removing duplicates
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < numbers.length - 1; i++) {
for (int j = i + 1; j < numbers.length; j++) {
// save the extracted numbers into the HashSet(set)
set.add(numbers[i] + numbers[j]);
}
}
// Convert HashSet to int[], sort it in ASC order.
return set.stream().sorted().mapToInt(Integer::intValue).toArray();
}
}
Code Summary
1.The HashSet is used to remove duplicates.
2. For each combination of numbers(2 pairs) represented by i-th and j-th values
in the array, their sum is calculated and added to HashSet(Saved)
3.set(HashSet) is converted to int[] using stream.
// Using Stream to convert List(numbers) to primitive int array.
int[] primitiveIntArray = numbers.stream()
.mapToInt(Integer::intValue)
.toArray();
// Using Stream to convert HashSet(set) to primitive int array.
int[] primitiveIntArray = set.stream().sorted()
.mapToInt(Integer::intValue)
.toArray();
Stream API
Stream API, introduced in Java 8, allows for operations on sequential elements.
It focuses on abstraction rather than implementation, promoting readable code.
By utilizing Stream API, Code becomes more modular and maintenance is more efficient.
List<Integer> numbers :
- numbers.stream(): creates "Stream<Integer>" from the Integer List (numbers)
mapToInt(Integer::intValue) : this code snippet transforms each Integer objects(wrapper class) in the Stream to its corresponding primitive int value.
(mapping Integer of List in the Stream to primitive int value)
- toArray() : collects primitive int values from stream into an int[] array.
HashSet<Integer> set
set.stream() : creates "Stream<Integer>" from the Integer HashSet(set)
- sorted() : Sorting the elements before mapping them to int values ensures that the resulting array is sorted in natural order, which is ascending order.
mapToInt(Integer::intValue) : this code snippet transforms each Integer objects(wrapper class) in the Stream to its corresponding primitive int value.
(mapping Integer of HashSet in the Stream to primitive int value)
- toArray : collects primitive int values from stream into an int[] array.
Subscribe to my newsletter
Read articles from Byung Joo Jeong directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by