JV
PY

Java to Python

10 lessons

Progress0%
1Variables & Types2Classes & OOP3Collections4Exception Handling5File I/O6Functional Programming7Duck Typing and Protocols8Python Ecosystem9Type Hints and Static Analysis10Context Managers and Resources
All Mirror Courses
JV
PY
Collections
MirrorLesson 3 of 10
Lesson 3

Collections

Lists, dicts, and functional operations

Introduction

In this lesson, you'll learn about collections in Python. Coming from Java, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JV
From Java:

In Java, you're familiar with lists, dicts, and functional operations.

PY
In Python:

Python has its own approach to lists, dicts, and functional operations, which we'll explore step by step.

The Python Way

Let's see how Python handles this concept. Here's a typical example:

PY
Python Example
nums = [1, 2, 3, 4, 5]
nums.append(6)

ages = {"Alice": 30, "Bob": 25}
val = ages.get("Alice", 0)

# List comprehensions (cleaner than streams)
doubled = [n * 2 for n in nums]
evens = [n for n in nums if n % 2 == 0]
total = sum(nums)

# Functional style
doubled2 = list(map(lambda n: n * 2, nums))
evens2 = list(filter(lambda n: n % 2 == 0, nums))

# Dict comprehension
square_map = {n: n**2 for n in range(6)}

Comparing to Java

Here's how you might have written similar code in Java:

JV
Java (What you know)
import java.util.*;
import java.util.stream.*;

List<Integer> nums = new ArrayList<>(Arrays.asList(1,2,3,4,5));
nums.add(6);

Map<String, Integer> ages = new HashMap<>();
ages.put("Alice", 30);
ages.put("Bob", 25);
int val = ages.getOrDefault("Alice", 0);

// Stream API
List<Integer> doubled = nums.stream()
    .map(n -> n * 2)
    .collect(Collectors.toList());

List<Integer> evens = nums.stream()
    .filter(n -> n % 2 == 0)
    .collect(Collectors.toList());

int total = nums.stream().mapToInt(i->i).sum();
Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Python list replaces ArrayList; no type parameter needed

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Python dict replaces HashMap; simpler syntax

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Python list comprehensions replace Java Stream API

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

PY
In Python:

Python sum(), min(), max() are built-in; Java needs stream().sum()

Step-by-Step Breakdown

1. Lists vs ArrayList

Python's built-in list is dynamic and needs no imports. No generics — the list holds any type.

JV
Java
List<Integer> nums = new ArrayList<>(Arrays.asList(1,2,3));
PY
Python
nums = [1, 2, 3]

2. Comprehensions vs Streams

Python list comprehensions are more concise than Java Streams and avoid the .collect(Collectors.toList()) boilerplate.

JV
Java
nums.stream().map(n -> n*2).collect(Collectors.toList())
PY
Python
[n * 2 for n in nums]

3. Built-in Aggregates

Python has sum(), min(), max(), len() as built-in functions. No stream needed.

JV
Java
nums.stream().mapToInt(i->i).sum()
PY
Python
sum(nums)

Common Mistakes

When coming from Java, developers often make these mistakes:

  • Python list replaces ArrayList; no type parameter needed
  • Python dict replaces HashMap; simpler syntax
  • Python list comprehensions replace Java Stream API
Common Pitfall
Don't assume Python works exactly like Java. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • ArrayList → list (no imports, no generics)
  • HashMap → dict
  • Stream API → list comprehensions
  • sum/min/max/len are built-in functions
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Java code in Python to practice these concepts.
PreviousNext