|
| 1 | +package com.platzi.functional_student_practice._15_streams; |
| 2 | + |
| 3 | +import com.platzi.functional_teacher_theory._06_reference_operator.NombresUtils; |
| 4 | + |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.List; |
| 7 | +import java.util.function.Supplier; |
| 8 | +import java.util.stream.Stream; |
| 9 | + |
| 10 | +public class C23Y24Streams { |
| 11 | + public static void main(String[] args) { |
| 12 | + System.out.println("\n////////////////////////////////////////////////////////////////////////////////////"); |
| 13 | + System.out.println("\nCLASS 23\n"); |
| 14 | + |
| 15 | + // 1. forma comun de trabajar sobre listas, strings y collections |
| 16 | + List<String> courseList = NombresUtils.getList("Java!", "JavaScript", "FrontEnd", "Backend", "FullStack"); |
| 17 | + for (String course : Collections.unmodifiableList(courseList)) { |
| 18 | + String newCourseName = course.toLowerCase().replace("!", "!!!"); |
| 19 | + System.out.println("1. Cursos bucle sobre lista: " + newCourseName); |
| 20 | + } |
| 21 | + System.out.println("1. Lista original: " + courseList); |
| 22 | + |
| 23 | + // 2. genero un supplier que inicia streams nuevos cada vez que es llamado con la misma informacíón de origen |
| 24 | + Supplier<Stream<String>> streamSupplier = () -> Stream.of("Java!", "JavaScript", "FrontEnd", "Backend", "FullStack"); |
| 25 | + |
| 26 | + // 2b. resultado de map generando una lista de integers con el length de cada String |
| 27 | + Stream<Integer> courseLengthStream = streamSupplier.get().map(String::length); |
| 28 | + System.out.println("2b. Lista lengths sobre stream: " + courseLengthStream.toList()); |
| 29 | + |
| 30 | + // 2c. hago un map como en 2b, y imprimo el length mayor |
| 31 | + System.out.println("2c. Mayor length de stream: " + streamSupplier.get().map(String::length).max((x, y) -> y - x).orElse(-1)); |
| 32 | + |
| 33 | + // 2d. genero un supplier que inicia streams nuevos cada vez que es llamado con la misma informacíón de origen, hago un map como en 2b, y imprimo el length mayor |
| 34 | + Stream<String> emphasisCourses = streamSupplier.get().map(course -> course + "!"); |
| 35 | + Stream<String> justJavaCourses = emphasisCourses.filter(course -> course.contains("Java")); |
| 36 | + justJavaCourses.forEach(x -> System.out.println("2d. Rtdo final de streams sucesivos: " + x.replace("!", "!!!"))); |
| 37 | + |
| 38 | + System.out.println("////////////////////////////////////////////////////////////////////////////////////"); |
| 39 | + System.out.println("\nCLASS 24\n"); |
| 40 | + |
| 41 | + //2e. genero un stream directamente de la lista original y le aplico un metodo peak en el medio del proceso |
| 42 | + Stream<String> coursesStream2 = courseList.stream(); |
| 43 | + |
| 44 | + peakItems(coursesStream2.filter(course -> course.contains("Java"))) |
| 45 | + .map(course -> course + "???") |
| 46 | + .forEach(x -> System.out.println("2e. For each terminal: " + x)); |
| 47 | + } |
| 48 | + |
| 49 | + static <T> Stream<T> peakItems(Stream<T> stream){ |
| 50 | + return stream.peek(data -> System.out.println("2e. Peeked data: " + data)); |
| 51 | + } |
| 52 | +} |
0 commit comments