C#
JV

C# to Java

10 lessons

Progress0%
1Introduction2Type Systems3Properties & Getters4Generics5Collections6LINQ to Streams7Async Programming8Ecosystem9Modern Java Features10Concurrency
All Mirror Courses
C#
JV
Collections
MirrorLesson 5 of 10
Lesson 5

Collections

Collections

Introduction

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

Mirror Card
C#
From C#:

In C#, you're familiar with collections.

JV
In Java:

Java has its own approach to collections, 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
ArrayList<String> names = new ArrayList<>();
HashMap<String, Integer> scores = new HashMap<>();
HashSet<Integer> unique = new HashSet<>();

names.add("Alice");
scores.put("Bob", 95);
unique.add(42);

int count = names.size();
boolean has = scores.containsKey("Bob");

Comparing to C#

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

C#
C# (What you know)
List<string> names = new();
Dictionary<string, int> scores = new();
HashSet<int> unique = new();

names.Add("Alice");
scores["Bob"] = 95;
unique.Add(42);

int count = names.Count;
bool has = scores.ContainsKey("Bob");
Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

JV
In Java:

C# Dictionary<K,V> maps directly to Java HashMap<K,V>

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

JV
In Java:

.Count (C#) vs .size() (Java) — Java uses a method, not a property

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

JV
In Java:

C# List<T> → Java ArrayList<T>; both are dynamic arrays

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

JV
In Java:

Java enhanced for-each loop works exactly like C# foreach

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

JV
In Java:

Java Stream API is the equivalent of LINQ for bulk operations

Step-by-Step Breakdown

1. Collection Name Mapping

The core collections map almost one-to-one. Only the names and small method names differ.

C#
C#
List<string> list = new();
Dictionary<string, int> dict = new();
HashSet<string> set = new();
Queue<int> queue = new();
Stack<int> stack = new();
JV
Java
List<String> list = new ArrayList<>();
Map<String, Integer> dict = new HashMap<>();
Set<String> set = new HashSet<>();
Queue<Integer> queue = new LinkedList<>();
Deque<Integer> stack = new ArrayDeque<>();
Rule of Thumb
Prefer declaring variables with the interface type (List, Map, Set) so you can swap implementations easily.

2. Common Method Differences

Most operation names differ slightly — Add/add, Remove/remove, Contains/contains — and Count is a property in C# but size() is a method in Java.

C#
C#
list.Add(item);
list.Remove(item);
list.Contains(item);
int n = list.Count;
dict["key"] = value;
JV
Java
list.add(item);
list.remove(item);
list.contains(item);
int n = list.size();
dict.put("key", value);
dict.get("key");

3. Iteration

Java's enhanced for-each loop is syntactically identical to C# foreach.

C#
C#
foreach (var name in names) {
    Console.WriteLine(name);
}
JV
Java
for (String name : names) {
    System.out.println(name);
}

// Or with forEach and lambda
names.forEach(name -> System.out.println(name));

4. Immutable Collections

Both platforms offer read-only/immutable collection factories.

C#
C#
var list = new List<string> { "a", "b" };
IReadOnlyList<string> ro = list.AsReadOnly();
// or: List.of equivalent
var immutable = ImmutableList.Create("a", "b");
JV
Java
// Java 9+ List.of creates immutable lists
List<String> immutable = List.of("a", "b");
Map<String, Integer> immutableMap = Map.of("x", 1);

// Unmodifiable view (like AsReadOnly)
List<String> ro = Collections.unmodifiableList(list);
Common Pitfall
List.of() in Java throws UnsupportedOperationException on any mutation — it is truly immutable, not just read-only.

Common Mistakes

When coming from C#, developers often make these mistakes:

  • C# Dictionary<K,V> maps directly to Java HashMap<K,V>
  • .Count (C#) vs .size() (Java) — Java uses a method, not a property
  • C# List<T> → Java ArrayList<T>; both are dynamic arrays
Common Pitfall
Don't assume Java works exactly like C#. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Dictionary → HashMap, List → ArrayList, HashSet → HashSet
  • .Count (property) → .size() (method)
  • Add/Remove/Contains → add/remove/contains
  • Use List.of() for immutable collections (Java 9+)
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C# code in Java to practice these concepts.
PreviousNext