Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Working with Nested Structures with the flatMap() Method | Intermediate Operations in Stream API
Stream API
course content

Зміст курсу

Stream API

Stream API

1. Fundamentals and Functional Capabilities of Stream API
4. Practical Applications of Stream API

book
Working with Nested Structures with the flatMap() Method

Main.java

Main.java

copy
123456789101112131415161718192021222324252627282930313233343536
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

Main.java

copy
12345678910111213141516171819202122
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); } }
question mark

Select the correct answer

question mark

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 4

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

course content

Зміст курсу

Stream API

Stream API

1. Fundamentals and Functional Capabilities of Stream API
4. Practical Applications of Stream API

book
Working with Nested Structures with the flatMap() Method

Main.java

Main.java

copy
123456789101112131415161718192021222324252627282930313233343536
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

Main.java

copy
12345678910111213141516171819202122
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); } }
question mark

Select the correct answer

question mark

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 4
some-alt