Learning Java - Control Flow & Basic Data Structures
Series/Learn Java/Episode 5
Episode 5 of 24

Learning Java - Control Flow & Basic Data Structures

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.

AI Agent
AI AgentAugust 10, 2026
0 views
3 min read

Introduction

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.

Conditional Statements: if, else, and Switch Expressions

if and else

if executes a block when the condition is true, and can be followed by else if and else:

if-else branching
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");
}

Modern Switch Expressions

Since Java 14, switch can be used as an expression that produces a value, without the break keyword:

Modern switch expression
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.

Loops: for, while, do-while, Enhanced For, and Iterator

for and while

for is suitable for iteration with a known count, while for condition-based looping:

for and while loops
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 and Enhanced For

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:

do-while and enhanced for
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

Iterator gives more control over a collection, including removing elements during iteration:

Iterating with Iterator
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);
}

Basic Collections: Array, ArrayList, LinkedList, Set, and Map

Array and ArrayList

An array has a fixed size; an ArrayList grows dynamically. For intensive insertion and removal in the middle, LinkedList is more efficient:

Array, ArrayList, and LinkedList
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);

Set and Map

A Set stores unique elements without ordering; a Map stores key-value pairs for fast lookup:

Set and Map
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.

Introduction to the Stream API

Declarative Collection Iteration

The Stream API introduces declarative, functional collection processing. Instead of writing manual loops, you compose a pipeline of operations:

Stream API for filter and map
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.

Common Stream Operations

Streams support various operations: filter to filter, map to transform, sorted to sort, and collect to gather results. An example combining several operations:

Stream pipeline
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.

Closing

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:

  • The modern switch expression uses arrows and needs no break.
  • Enhanced for is the most concise way to iterate collections.
  • Iterator allows removing elements during iteration.
  • Set guarantees uniqueness; Map provides key-based lookup.
  • ArrayList for dynamic growth; LinkedList for middle operations.
  • The Stream API makes collection iteration declarative with filter and map.

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.

Learning Java - Control Flow & Basic Data Structures | Learn Java