Java Programming Labs

Master advanced collections, functional programming with streams, and concurrent programming basics.

Collections, Streams & Multithreading - Module 5

Learn HashMap/HashSet, lambda expressions, Stream API, and thread fundamentals.

Lab 13: HashMap & HashSet Collections
Advanced
Coding Challenge
Your Task: Master Java's HashMap and HashSet collections for efficient key-value storage and unique element management.

Detailed Requirements:
1. Import java.util.* for collection classes.

2. Create and use HashMap:
   • HashMap<String, Integer> scores = new HashMap<>();
   • put(key, value) - add/update entries
   • get(key) - retrieve value by key
   • containsKey(key) - check if key exists
   • keySet(), values(), entrySet() - iterate

3. Create and use HashSet:
   • HashSet<String> names = new HashSet<>();
   • add(element) - adds unique elements only
   • contains(element) - check membership
   • remove(element) - remove element

4. Iterate through collections using for-each or entrySet().

Key Concepts:
• HashMap: Key-Value pairs, O(1) lookup, keys must be unique
• HashSet: Unique elements only, no duplicates, O(1) contains
• Both use hashCode() and equals() internally

Expected Output:
--- HashMap Demo --- Scores: {Alice=95, Bob=87, Charlie=92} Alice's score: 95 Contains Bob? true All entries: Alice -> 95 Bob -> 87 Charlie -> 92 --- HashSet Demo --- Unique names: [Alice, Bob, Charlie] Add duplicate 'Alice': false Contains 'Bob'? true After removing Bob: [Alice, Charlie]

Requirements Checklist

Import java.util.*
Create HashMap with generics
Use put(), get(), containsKey()
Iterate with entrySet() or keySet()
Create HashSet with generics
Use add(), contains(), remove() on HashSet
Output
// Click "Run Code" to compile and execute
Hints & Tips
• HashMap: HashMap<K, V> map = new HashMap<>();
• Add: map.put("key", value); Get: map.get("key");
• Iterate: for (Map.Entry<K,V> e : map.entrySet()) { }
• HashSet: set.add() returns false if duplicate
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 14: Lambda Expressions & Streams
Advanced
Coding Challenge
Your Task: Master functional programming in Java using lambda expressions and the Stream API for elegant data processing.

Detailed Requirements:
1. Import required classes:
   import java.util.*;
   import java.util.stream.*;

2. Lambda Expression Syntax:
   • (params) -> expression - single expression
   • (params) -> { statements; } - code block
   • Example: list.forEach(x -> System.out.println(x));

3. Stream Operations:
   • filter(predicate) - select elements matching condition
   • map(function) - transform each element
   • sorted() - sort elements
   • collect(Collectors.toList()) - gather results
   • reduce() - combine elements into single result

4. Common patterns:
   • list.stream().filter(x -> x > 5).collect(Collectors.toList())
   • list.stream().map(x -> x * 2).toList()
   • list.stream().mapToInt(x -> x).sum()

Expected Output:
Original: [3, 1, 4, 1, 5, 9, 2, 6] Filtered (>3): [4, 5, 9, 6] Doubled: [8, 10, 18, 12] Sorted: [4, 5, 6, 9] Sum: 31 Names: [Alice, Bob, Charlie, David] Names starting with 'A' or 'C': [Alice, Charlie] Uppercase: [ALICE, CHARLIE]

Requirements Checklist

Import java.util.stream.*
Use lambda expression with ->
Use filter() to select elements
Use map() to transform elements
Use sorted() or reduce()
Use collect() or toList()
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Lambda: (x) -> x * 2 or x -> x * 2
• Filter: .filter(x -> condition)
• Map: .map(x -> transform(x))
• Collect: .collect(Collectors.toList())
• Sum: .mapToInt(x -> x).sum()
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below

Lab 15: Multithreading Basics
Expert
Coding Challenge
Your Task: Learn Java multithreading fundamentals by creating threads, managing their lifecycle, and understanding basic synchronization.

Detailed Requirements:
1. Create thread by extending Thread class:
   class MyThread extends Thread { public void run() { } }
   Override the run() method with thread logic

2. Create thread using Runnable interface:
   class MyRunnable implements Runnable { public void run() { } }
   Thread t = new Thread(new MyRunnable());

3. Thread lifecycle methods:
   • start() - begin thread execution
   • join() - wait for thread to complete
   • sleep(millis) - pause execution
   • getName() - get thread name

4. Lambda shorthand:
   Thread t = new Thread(() -> { /* code */ });

Key Concepts:
• Thread = independent path of execution
• Main thread spawns child threads
• Threads run concurrently (may interleave)
• Use join() to ensure completion before continuing

Expected Output (order may vary):
Main thread starting... Thread-A: Count 0 Thread-B: Count 0 Thread-A: Count 1 Thread-B: Count 1 Thread-A: Count 2 Thread-B: Count 2 Thread-A finished! Thread-B finished! Lambda thread running... Main thread finished!

Requirements Checklist

Create class extending Thread
Override run() method
Use start() to begin thread
Implement Runnable interface OR use lambda
Use Thread.sleep() for delay
Use join() to wait for completion
Output
// Click "Run Code" to compile and execute
Hints & Tips
• Extend: class MyThread extends Thread { public void run() { } }
• Start: thread.start(); (NOT run() directly!)
• Sleep: Thread.sleep(1000); in try-catch
• Lambda: new Thread(() -> { code });
• Wait: thread.join(); blocks until thread finishes
Progress: 0/6
Score: 0/100
0%

Lab Results

Review feedback below