Lists to Arrays
Lists to Arrays
Introduction
In this lesson, you'll learn about lists to arrays in Java. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Python, you're familiar with lists to arrays.
Java has its own approach to lists to arrays, which we'll explore step by step.
The Java Way
Let's see how Java handles this concept. Here's a typical example:
import java.util.*;
import java.util.stream.*;
// Fixed-size primitive array
int[] arr = {1, 2, 3};
// Dynamic list (like Python list)
List<Integer> nums = new ArrayList<>(Arrays.asList(1, 2, 3));
nums.add(4);
Collections.sort(nums);
// Stream map (list comprehension equivalent)
List<Integer> doubled = nums.stream()
.map(x -> x * 2)
.collect(Collectors.toList());
// Sublist (like slicing)
List<Integer> part = nums.subList(1, 3);Comparing to Python
Here's how you might have written similar code in Python:
nums = [1, 2, 3]
# Dynamic — grows automatically
nums.append(4)
nums.sort()
# List comprehension
doubled = [x * 2 for x in nums]
# Slicing
part = nums[1:3]You may be used to different syntax or behavior.
Python list is always dynamic; Java int[] is fixed-size — use ArrayList for dynamic lists
You may be used to different syntax or behavior.
Python list comprehension → Java Stream .map().collect()
You may be used to different syntax or behavior.
Python .append() → Java .add()
You may be used to different syntax or behavior.
Python slicing → Java .subList(from, to) or Arrays.copyOfRange()
You may be used to different syntax or behavior.
import java.util.* is required for ArrayList and Collections
Step-by-Step Breakdown
1. Fixed Array vs ArrayList
Java int[] has a fixed size set at creation. ArrayList<Integer> grows dynamically like a Python list.
nums = [1, 2, 3]
nums.append(4)List<Integer> nums = new ArrayList<>();
nums.add(1); nums.add(2); nums.add(3);
nums.add(4);2. Sorting
Use Collections.sort() for ArrayList or Arrays.sort() for primitive arrays.
nums.sort()
nums.sort(reverse=True)Collections.sort(nums); // ascending
Collections.sort(nums, Collections.reverseOrder()); // descending3. Streams as Comprehensions
Java Streams are the functional equivalent of Python list comprehensions: filter → filter, map → map, collect → result list.
doubled = [x * 2 for x in nums]
evens = [x for x in nums if x % 2 == 0]List<Integer> doubled = nums.stream().map(x -> x * 2).collect(Collectors.toList());
List<Integer> evens = nums.stream().filter(x -> x % 2 == 0).collect(Collectors.toList());4. Sublist (Slicing)
Java subList(from, to) returns a view of the list — modifications affect the original. Use new ArrayList<>(sub) for an independent copy.
part = nums[1:3]List<Integer> part = new ArrayList<>(nums.subList(1, 3));Common Mistakes
When coming from Python, developers often make these mistakes:
- Python list is always dynamic; Java int[] is fixed-size — use ArrayList for dynamic lists
- Python list comprehension → Java Stream .map().collect()
- Python .append() → Java .add()
Key Takeaways
- Use ArrayList<T> for dynamic lists; int[] for fixed-size arrays
- Stream .map/.filter/.collect replaces list comprehensions
- .add() ≈ .append(), Collections.sort() ≈ .sort()