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.
In C#, you're familiar with collections.
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:
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#:
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");You may be used to different syntax or behavior.
C# Dictionary<K,V> maps directly to Java HashMap<K,V>
You may be used to different syntax or behavior.
.Count (C#) vs .size() (Java) — Java uses a method, not a property
You may be used to different syntax or behavior.
C# List<T> → Java ArrayList<T>; both are dynamic arrays
You may be used to different syntax or behavior.
Java enhanced for-each loop works exactly like C# foreach
You may be used to different syntax or behavior.
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.
List<string> list = new();
Dictionary<string, int> dict = new();
HashSet<string> set = new();
Queue<int> queue = new();
Stack<int> stack = new();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<>();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.
list.Add(item);
list.Remove(item);
list.Contains(item);
int n = list.Count;
dict["key"] = value;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.
foreach (var name in names) {
Console.WriteLine(name);
}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.
var list = new List<string> { "a", "b" };
IReadOnlyList<string> ro = list.AsReadOnly();
// or: List.of equivalent
var immutable = ImmutableList.Create("a", "b");// 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 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
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+)