Skip to content

Commit 4e0d379

Browse files
committed
class 23 24 streams
1 parent f35f34f commit 4e0d379

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

312 KB
Loading
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

modules/src/main/java/com/platzi/functional_teacher_theory/_06_reference_operator/NombresUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class NombresUtils {
88
public static void main(String[] args) {
99
List<String> profesores = getList("Nicolas", "Juan", "Zulema");
1010

11-
Consumer<String> printer = text -> System.out.println(text);
11+
Consumer<String> printer = System.out::println;
1212
profesores.forEach(printer);
1313
System.out.println("//////////");
1414
profesores.forEach(System.out::println);

0 commit comments

Comments
 (0)