Зміст курсу
Stream API
Stream API
1. Fundamentals and Functional Capabilities of Stream API
ArchitecturePrinciplesWhat Is a Functional Interface?Predicate: Data FilteringChallenge: Filtering Corporate Email AddressesFunction: Data TransformationChallenge: Income Tax CalculationComparable: Natural Ordering of DataComparator: Custom Comparison of DataChallenge: Sorting Employees Consumer: Processing Data Supplier: Data GenerationBi-versions of Functional InterfacesChallenge: Filtering Users by Two CriteriaBinaryOperator: Combining Two Values
2. Intermediate Operations in Stream API
Transforming Elements with the map() MethodFiltering Elements with the filter() MethodChallenge: Factory Product FilteringWorking with Nested Structures with the flatMap() MethodArranging Items in Order with the sorted() MethodChallenge: Selecting the Best Cars on the Production LineEliminating Duplicates with the distinct() MethodChallenge: Factory Quality ControlRestricting and Skipping Elements with the limit() and skip() MethodsChallenge: Finding the Top 3 Hardest-Working EmployeesIntermediate Processing with the peek() Method
3. Terminal Operations in the Stream API
collect() Gathering Stream Elements into a CollectionChallenge: Build a Custom Collector for Category CountingCollectors Utility Class for Stream APIProcessing Elements with the forEach() MethodHandling Values with the Optional ClassAggregating Elements with the reduce() MethodChallenge: Calculating Total Cost with Discounts and TaxCalculating Stream Statistics with count(), max(), and min()Retrieving Stream Summary Metrics with summaryStatistics() MethodRetrieving Elements from a Stream with findFirst() and findAny()Challenge: Selecting Random Products Within a CategoryChecking Stream Elements Against a Condition with allMatch()Challenge: Ensuring Fast Delivery for Expensive Products
4. Practical Applications of Stream API
Working with Nested Structures with the flatMap() Method
Main.java
package com.example; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<Factory> factories = Arrays.asList( new Factory("SteelWorks", Arrays.asList("Drill", "Excavator", "Bulldozer")), new Factory("AutoParts", Arrays.asList("Carburetor", "Piston", "Transmission")), new Factory("ToolMakers", Arrays.asList("Screwdriver", "Wrench", "Hammer")) ); List<String> productList = factories.stream() .flatMap(factory -> factory.getProducts().stream()) // Flatten all product lists into a single stream .map(String::toUpperCase) // Convert product names to uppercase .toList(); // Collect results into a list System.out.println(productList); } } class Factory { private String name; private List<String> products; public Factory(String name, List<String> products) { this.name = name; this.products = products; } public List<String> getProducts() { return products; } }
Main.java
package com.example; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { // List of production lines with their products List<List<String>> productionLines = Arrays.asList( Arrays.asList("Tire", "Rim", "Shock Absorber"), Arrays.asList("Frame", "Tread", "Ball Joint"), Arrays.asList("Brakes", "Steering System") ); // Get a single list of all products manufactured in the factory List<String> allProducts = productionLines.stream() .flatMap(line -> line.stream()) // Convert each production line's product list into a stream .toList(); // Collect all products into a single list System.out.println(allProducts); } }
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 2. Розділ 4