Skip to content

Commit 27fec99

Browse files
committed
Definicion de operadores intermedios agregada
1 parent 25ee996 commit 27fec99

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.platzi.functional._18_intermediate_ops;
2+
3+
import com.platzi.functional.util.Utils;
4+
5+
import java.util.Collection;
6+
import java.util.List;
7+
import java.util.stream.Stream;
8+
9+
public class IntermediateOperations {
10+
static void operaciones() {
11+
/*
12+
Las operaciones intermedias, como se vio en el modulo anterior, son operaciones que
13+
devuelven un nuevo Stream.
14+
15+
Estas operaciones son:
16+
17+
- filter()
18+
- map()
19+
- flatMap()
20+
- distinct()
21+
- limit()
22+
- peek()
23+
*/
24+
25+
//filter
26+
//Toma un Predicate que indica si debemos o no considerar el elemento para el nuevo Stream
27+
Stream<Integer> evenNumbersStream = Stream.iterate(0, i -> i + 1);
28+
evenNumbersStream.filter(i -> i % 2 == 0); //Solo los numeros pares.
29+
30+
31+
//
32+
//
33+
//
34+
//
35+
//
36+
//
37+
//
38+
39+
//map
40+
//Convertir un Stream de tipo T a un Stream de tipo V. Es posible que T y V sean el mismo tipo:
41+
Stream<String> namesStream = Stream.of("Sinuhe", "Brenn", "Ricardo", "Sebastian", "Luisa");
42+
Stream<Integer> lengthNameStream = namesStream.map(String::length);
43+
44+
//flatMap
45+
/*
46+
Como mencionamos en el modulo sobre Listeners, flatMap convierte un Stream complejo en un Stream lineal.
47+
Es decir, flatMap opera sobre un Stream que contenga datos "anidados" como puede ser una Collection o un Array,
48+
tomando una lambda que retorne otro Stream de los datos anidados y concatenando todos los datos en un solo stream.
49+
50+
Si nuestro Stream inicial tenia:
51+
Stream{ List{ "Node.js", "JavaScript"}, List{"Android", "Kotlin"}, List{"JavaSE 8", "Java FP"}}
52+
53+
Aplicar flatMap retorna:
54+
Stream{ "Node.js", "JavaScript", "Android", "Kotlin", "JavaSE 8", "Java FP" }
55+
*/
56+
List<String> nodeCourses = Utils.getListOf("Node.js", "Express.js", "Eventloop");
57+
List<String> javaCourses = Utils.getListOf("Spring", "Maven", "Gradle", "Funtional");
58+
59+
Stream<List<String>> coursesListStream = Stream.of(nodeCourses, javaCourses);
60+
Stream<String> coursesStream = coursesListStream.flatMap(Collection::stream);
61+
62+
63+
//
64+
//
65+
//
66+
//
67+
//
68+
//
69+
//distinct
70+
//Genera un Stream de elementos unicos tomando como fuente: Object.equals
71+
Stream<String> heroesNamesStream = Stream.of("Peter", "Logan", "Luisa", "Clark", "Gwen", "Logan", "Peter");
72+
Stream<String> uniqueHeroesNamesStream = heroesNamesStream.distinct();// "Peter", "Logan", "Luisa", "Clark", "Gwen"
73+
74+
75+
//
76+
//
77+
//
78+
//
79+
//
80+
//limit
81+
//Limita los elementos del Stream al numero indicado.
82+
Stream<String> justTwoHeroes = uniqueHeroesNamesStream.limit(2);
83+
84+
//
85+
//
86+
//
87+
//peek
88+
/*
89+
Recibe un Consumer para operar sobre cada elemento del Stream pero manteniendo el tipo del Stream
90+
Es una manera sencilla de hacer una operacion intermedia sobre el Stream sin alterarlo (idealmente)
91+
*/
92+
Stream<String> choosenTwoHeroes = justTwoHeroes.peek(heroe -> System.out.println("Un heroe ha sido elegido: " + heroe));
93+
94+
}
95+
}

0 commit comments

Comments
 (0)