CodeBlanks Logo Icon CodeBlanks

Putting it together

Combine all advanced topics into a working app.

Putting it together

In the final lesson you combine all layers. In main you will run this flow:

  1. Apply a stream filter on log and print the count.
  2. Compute the total severity with ExecutorService and print it.
  3. Call describe via reflection and print the result.

These steps are all deterministic, so the output is the same every time. This shows how an advanced app is built layer by layer.

Practice Combine the whole flow

Use the previous lesson's solution. In main add: compute log.stream().filter(e -> e.severity > 2).count() and print it. Compute the total severity with ExecutorService, get it with pool.submit(task).get() and print it. Call describe via reflection and print the result.

Starter code

Main.java
import java.util.ArrayList;
import java.util.concurrent.*;
import java.lang.reflect.*;
import java.util.stream.Collectors;

class Event {
    String type;
    int severity;
    Event(String type, int severity) {
        this.type = type;
        this.severity = severity;
    }
}

@interface Logged {
    String value();
}

class EventService {
    @Logged("audit")
    public String describe() {
        return "event-service";
    }
}

public class Main {
    public static void main(String[] args) throws Exception {
        ArrayList<Event> log = new ArrayList<>();
        log.add(new Event("info", 1));
        log.add(new Event("error", 5));
        log.add(new Event("warn", 3));
        // Combine: stream filter, concurrent total, reflection describe
    }
}

Sample solution

Main.java
import java.util.ArrayList;
import java.util.concurrent.*;
import java.lang.reflect.*;
import java.util.stream.Collectors;

class Event {
    String type;
    int severity;
    Event(String type, int severity) {
        this.type = type;
        this.severity = severity;
    }
}

@interface Logged {
    String value();
}

class EventService {
    @Logged("audit")
    public String describe() {
        return "event-service";
    }
}

public class Main {
    public static void main(String[] args) throws Exception {
        ArrayList<Event> log = new ArrayList<>();
        log.add(new Event("info", 1));
        log.add(new Event("error", 5));
        log.add(new Event("warn", 3));
        long count = log.stream().filter(e -> e.severity > 2).count();
        ExecutorService pool = Executors.newSingleThreadExecutor();
        Callable<Integer> task = () -> {
            int sum = 0;
            for (Event e : log) sum += e.severity;
            return sum;
        };
        int total = pool.submit(task).get();
        pool.shutdown();
        EventService service = new EventService();
        Method m = EventService.class.getMethod("describe");
        String result = (String) m.invoke(service);
        System.out.println(count);
        System.out.println(total);
        System.out.println(result);
    }
}

Expected output

2
9
event-service

Practice Summary: a mini integration

Define a new Summary class. Give it a void run() method that builds an ArrayList<Event> with three events and prints stream().filter(e -> e.severity > 2).count() and the for-each total severity on separate lines. In main, call new Summary().run().

Starter code

Main.java
import java.util.ArrayList;

class Event {
    String type;
    int severity;
    Event(String type, int severity) {
        this.type = type;
        this.severity = severity;
    }
}

// Define the Summary class

public class Main {
    public static void main(String[] args) {
        // Run a Summary instance
    }
}

Sample solution

Main.java
import java.util.ArrayList;

class Event {
    String type;
    int severity;
    Event(String type, int severity) {
        this.type = type;
        this.severity = severity;
    }
}

class Summary {
    void run() {
        ArrayList<Event> log = new ArrayList<>();
        log.add(new Event("info", 1));
        log.add(new Event("error", 5));
        log.add(new Event("warn", 3));
        long count = log.stream().filter(e -> e.severity > 2).count();
        int total = 0;
        for (Event e : log) total += e.severity;
        System.out.println(count);
        System.out.println(total);
    }
}

public class Main {
    public static void main(String[] args) {
        new Summary().run();
    }
}

Expected output

2
9

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which layers run together in the combining lesson?

  • Only Stream
  • Stream, Concurrent and Reflection Correct answer
  • Only Concurrent
  • Only Reflection

Explanation: Stream, Concurrent and Reflection layers run in the same main.

2 Complete the Future.get call

Question
ExecutorService pool = Executors.newSingleThreadExecutor();
Callable<Integer> task = () -> { /* toplam */ return 9; };
int total = pool.submit(task).____();
Answer
ExecutorService pool = Executors.newSingleThreadExecutor();
Callable<Integer> task = () -> { /* toplam */ return 9; };
int total = pool.submit(task).get();

Explanation: get() blocks until the result is ready and returns it.

3 Match the project summary

Term Meaning
Event Model class
Stream filter High severity events
ExecutorService Concurrent total
@Logged Reflection marker

All lessons in Advanced