PY
JV

Python to Java

11 lessons

Progress0%
1Introduction2Variables & Types3Functions to Methods4Lists to Arrays5Dicts to Maps6Classes & OOP7Inheritance8Exception Handling9Modules to Packages10Ecosystem11Modern Java Features
All Mirror Courses
PY
JV
Lists to Arrays
MirrorLesson 4 of 11
Lesson 4

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.

Mirror Card
PY
From Python:

In Python, you're familiar with lists to arrays.

JV
In Java:

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:

JV
Java 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:

PY
Python (What you know)
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]
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python list is always dynamic; Java int[] is fixed-size — use ArrayList for dynamic lists

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python list comprehension → Java Stream .map().collect()

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python .append() → Java .add()

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

Python slicing → Java .subList(from, to) or Arrays.copyOfRange()

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JV
In Java:

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.

PY
Python
nums = [1, 2, 3]
nums.append(4)
JV
Java
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.

PY
Python
nums.sort()
nums.sort(reverse=True)
JV
Java
Collections.sort(nums);                        // ascending
Collections.sort(nums, Collections.reverseOrder()); // descending

3. Streams as Comprehensions

Java Streams are the functional equivalent of Python list comprehensions: filter → filter, map → map, collect → result list.

PY
Python
doubled = [x * 2 for x in nums]
evens  = [x for x in nums if x % 2 == 0]
JV
Java
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());
Common Pitfall
Streams are lazy and can only be consumed once. Call collect() to produce a reusable List.

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.

PY
Python
part = nums[1:3]
JV
Java
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()
Common Pitfall
Don't assume Java works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

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()
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in Java to practice these concepts.
PreviousNext