Java collections, generics and streams
You process larger data with safe, readable code using the Java collections framework, generics and the Stream API.
Level 201 · 22 lessons · 24 practice labs
Lessons
Collections
- List, Set and Map: choosing the right collection: You tell apart when to use an ordered list, a duplicate-free set and a key-value map.
- Queue, Deque and PriorityQueue
- equals and hashCode: why do objects go missing?: You understand how HashMap and HashSet find objects and write both methods correctly to prevent lost entries.
- Sorting: Comparable and Comparator: You sort object lists by natural order and by your own criteria.
- Iterators and ConcurrentModificationException
- Immutable collections and defensive copying: You build immutable collections with List.of and stop a list you hand out from corrupting data inside your class.
- Short task: keep a leaderboard in the right collection
Generics and the functional style
- Generics: type-safe classes and methods: You write generic code that works safely across types and set bounds on those types.
- Lambda expressions and functional interfaces: You pass behaviour as a parameter using lambdas and interfaces such as Function and Predicate.
- Stream basics: filter, map and reduction: You filter, transform and reduce a collection without writing loops.
- Grouping and counting with Collectors: You group records by a field, count them and produce a summary table.
- flatMap and parallel streams: when do they help?
- Avoiding null errors with Optional: You express that a value may be missing with Optional and cut down on unexpected null errors.
- Modelling data with records and enums: You write immutable data carriers with record and fixed sets of choices with enum, briefly and safely.
- Short task: a regional report from sales records with streams
Resilient applications
- Threads and ExecutorService: You run tasks in parallel with Thread and ExecutorService and see why shared data causes race conditions.
- Concurrent access: synchronized and ConcurrentHashMap: You protect shared data with locks and concurrent collections to get consistent results.
- Loading objects safely: serialization risks: You understand the danger of reading objects from untrusted sources and choose safer paths such as ObjectInputFilter and JSON.
- Logging: java.util.logging and levels: You write leveled logs instead of System.out and learn to keep sensitive data out of them.
- Unit tests with JUnit: You test method behaviour automatically with JUnit, including the error cases.
- Short task: a tested, concurrent word counter
Capstone
- Capstone: a duplicate-file finder using SHA-256: You write a program that groups a folder’s files by digest and reports duplicates, using collections and streams.
Practice labs
- Counting unique visitors with a Set: Produce the unique visitor count and a list preserving first-seen order from visit records with the right Set type.
- Keeping undo history in a Deque: Write a history with ArrayDeque that keeps the last twenty operations and supports undo and redo.
- Keeping a leaderboard in the right collection: Keep a leaderboard with frequent score updates in a sorted structure instead of sorting per query and answer the top ten and tie order correctly.
- Implementing Comparable for a version class: Write compareTo for a version class with major, minor and patch numbers and sort a list in natural order.
- Multi-field sorting with a Comparator chain: Sort employees by department, then salary descending and name ascending, placing null departments last.
- Finding objects that go missing in a HashMap: Fix records that cannot be found because equals was written without hashCode and the key is mutated later, making the key immutable.
- Removing expired entries with an Iterator: Fix a loop that throws ConcurrentModificationException when removing while iterating by using Iterator.remove or removeIf.
- Protecting a list leaking from a getter: Use defensive copies in the constructor and getter of an order class that returns its internal list and test that outside changes have no effect.
- Solving a bug from the view-versus-copy difference: Fix a report cache that breaks when the backing list changes although it returns an unmodifiable view by using List.copyOf, and pass the immutability tests.
- Writing a generic pair class: Write a generic pair class holding two values of different types and a generic method that swaps the elements.
- Passing rules through a functional interface: Take discount rules as parameters with Predicate and Function and combine them with and and andThen.
- Flexible copying with bounded wildcards: Generalise copy and sum methods that only work between lists of the same type with extends and super wildcards so that calls failing to compile now compile.
- Filtering and mapping with a stream: Write a stream that returns the e-mail addresses of active customers in lower case as an alphabetically sorted list.
- Grouping sales by region: Group Turnalı Perakende's sales records by region and produce total revenue and the best-selling product per region with Collectors.
- Using flatMap, Optional and parallel streams correctly: Flatten nested order items with flatMap, replace null checks with Optional and remove the race in a parallel stream that writes to a shared list.
- Modelling a shipment with a record and an enum: Define shipment data as a record and its status as an enum, checking in the compact constructor that weight is positive.
- Writing a concurrent word counter correctly: Remove the HashMap race in a tool that counts files in parallel with ExecutorService using ConcurrentHashMap.merge and verify the result with a JUnit test.
- Closing unsafe object loading: Move a tool that reads its settings from a file with Java serialization to a validated text format, test that a file with an unexpected class is rejected and log the event.
- Keeping status counts in an EnumMap: Move code that counts orders by status from a string-keyed map to an EnumMap, define the allowed next statuses with EnumSet and report never-seen statuses as zero.
- Making a record truly immutable with constructor checks: Add validation and a defensive copy through a compact constructor to an order record that carries a list, so changes to the outside list do not affect the record and invalid amounts are rejected.
- Fixing a list modified during iteration: Fix code that throws ConcurrentModificationException when one thread iterates a listener list while another adds to it by using a suitable concurrent collection.
- Loading an expensive value only once: Stop the loader running several times for the same key in a cache written as check-then-put by using computeIfAbsent; the tests expect a single load across a hundred threads.
- Reading a typed list from JSON: Read a list of product records from a JSON file with full type information, raising an error on inputs with unknown fields instead of accepting them silently.
- Restricting loadable subtypes with an allow-list: Restrict a JSON reader that creates objects from a type name in the input so it accepts only the three defined notification types, rejecting any type name not on the list.
About this course
You process larger data with safe, readable code using the Java collections framework, generics and the Stream API.
What you will learn
- Collections
- Generics and the functional style
- Resilient applications
- Capstone
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.