This episode covers control flow and basic data structures: if, else, and modern switch expressions, for, while, do-while, enhanced for, and iterator loops, the basic collections array, ArrayList, LinkedList, Set, and Map, and an introduction to the Stream API for iterating collections.

A useful program almost always needs to make decisions and repeat work. Episode 5 covers control flow and basic data structures — the two pillars that drive application logic. You will learn branching, loops, collections, and the Stream API.
Mastering this topic means you can write programs that respond to different conditions and process collections of data. Almost all production code revolves around these patterns, so do not rush — understand them well.
if executes a block when the condition is true, and can be followed by else if and else:
int nilai = 82;
if (nilai >= 90) {
System.out.println("Nilai A");
} else if (nilai >= 80) {
System.out.println("Nilai B");
} else {
System.out.println("Nilai C");
}Since Java 14, switch can be used as an expression that produces a value, without the break keyword:
String hari = "Sabtu";
String kategori = switch (hari) {
case "Sabtu", "Minggu" -> "Akhir pekan";
case "Senin" -> "Mulai minggu";
default -> "Hari biasa";
};
System.out.println(kategori);The switch expression uses the arrow -> and supports multiple labels at once separated by commas. It is far more concise and safer than the classic switch.
for is suitable for iteration with a known count, while for condition-based looping:
for (int i = 0; i < 5; i++) {
System.out.println("Iterasi " + i);
}
int counter = 0;
while (counter < 3) {
System.out.println("While " + counter);
counter++;
}do-while runs the block at least once before checking the condition. Enhanced for (for-each) is the most common way to iterate over collections:
int n = 1;
do {
System.out.println(n);
n++;
} while (n <= 3);
int[] angka = {10, 20, 30};
for (int a : angka) {
System.out.println(a);
}Iterator gives more control over a collection, including removing elements during iteration:
List<String> nama = new ArrayList<>(List.of("Andi", "Budi", "Citra"));
Iterator<String> it = nama.iterator();
while (it.hasNext()) {
String n = it.next();
System.out.println(n);
}An array has a fixed size; an ArrayList grows dynamically. For intensive insertion and removal in the middle, LinkedList is more efficient:
int[] angka = new int[3];
angka[0] = 7;
List<String> daftar = new ArrayList<>();
daftar.add("Java");
daftar.add("JVM");
List<Integer> antrian = new LinkedList<>();
antrian.add(1);A Set stores unique elements without ordering; a Map stores key-value pairs for fast lookup:
Set<String> kota = new HashSet<>();
kota.add("Jakarta");
kota.add("Bandung");
kota.add("Jakarta"); // duplikat diabaikan
Map<String, Integer> stok = new HashMap<>();
stok.put("Laptop", 12);
stok.put("Monitor", 30);
int jumlah = stok.get("Laptop");These collections are the foundation of almost every data structure you will encounter.
The Stream API introduces declarative, functional collection processing. Instead of writing manual loops, you compose a pipeline of operations:
List<Integer> angka = List.of(1, 2, 3, 4, 5, 6);
List<Integer> genap = angka.stream()
.filter(n -> n % 2 == 0)
.toList();
System.out.println(genap);angka.stream().filter(n -> n % 2 == 0) produces a stream containing only the even numbers, then .toList() collects them back into a list.
Streams support various operations: filter to filter, map to transform, sorted to sort, and collect to gather results. An example combining several operations:
List<String> hasil = List.of("apel", "jeruk", "pisang", "anggur")
.stream()
.filter(buah -> buah.length() > 4)
.map(String::toUpperCase)
.sorted()
.toList();The Stream API will reappear in many later episodes, so make sure you are comfortable with this pattern.
Episode 5 equips you with control flow and data structures: branching with if-else and modern switch expressions, for, while, do-while, enhanced for, and iterator loops, the array, List, Set, and Map collections, and an introduction to the Stream API.
Key takeaways:
In the next episode, episode 6, we will discuss OOP basics and class design — the concepts of objects and classes, encapsulation with accessors and mutators, inheritance, polymorphism, abstract classes, and modern interfaces with default methods, static methods, and functional interfaces. This is the core of the Java programming paradigm.