-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.java
175 lines (142 loc) · 6.38 KB
/
App.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.time.LocalDate;
import java.time.Period;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class App {
public static void main(String[] args) {
Person p1 = new Person(1, "Mito", LocalDate.of(1991, 1, 21));
Person p2 = new Person(2, "Code", LocalDate.of(1990, 2, 21));
Person p3 = new Person(3, "Jaime", LocalDate.of(1980, 6, 23));
Person p4 = new Person(4, "Duke", LocalDate.of(2019, 5, 15));
Person p5 = new Person(5, "James", LocalDate.of(2010, 1, 4));
Product pr1 = new Product(1, "Ceviche", 15.0);
Product pr2 = new Product(2, "Chilaquiles", 25.50);
Product pr3 = new Product(3, "Bandeja Paisa", 35.50);
Product pr4 = new Product(4, "Ceviche", 15.0);
List<Person> persons = Arrays.asList(p1, p2, p3, p4, p5);
List<Product> products = Arrays.asList(pr1, pr2, pr3, pr4);
persons.forEach(x-> {
// System.err.println(x.getName());
});
var xxxx =persons.stream().filter(x->x.getName().equals("Jaime"))
.collect(Collectors.toList());
List<String> listaNombre= persons.stream().
map(x->x.getName()).
filter(x->x.equals("Jaime") ? true : false)
.collect(Collectors.toList());
//?
/* */
Predicate pred= new Predicate<String>() {
@Override
public boolean test(String t) {
// TODO Auto-generated method stub
return false ;
}
};
Predicate<String> asd =(String x) -> false;
List<String> lost1 = Arrays.asList("XD","sadsd");
List<String> lost2 = new ArrayList<>(Arrays.asList("XD","sadsd"));
List<String> lost3 = List.of("xd");
List<String> lost4 = new ArrayList<>();
lost4.addAll(lost3);
System.out.println(lost4);
lost4.add("sd");
System.out.println(lost4);
Predicate<Person> predicatePersonas = p -> p.getName().equals("Jaime");
List<Person> nuevaLista = persons.stream().filter(predicatePersonas).toList();
Map<String,String> xd = new HashMap<>();
xd.values();
//App.printList(listaNombre);
// Lambda //method reference
// list.forEach(System.out::println);
/*for(int i = 0; i<persons.size(); i++){
System.out.println(persons.get(i));
}*/
/*for(Person p : persons){
System.out.println(p);
}*/
//persons.forEach(x -> System.out.println(x));
//persons.forEach(System.out::println);
// 1-Filter (param: Predicate)
List<Person> filteredList1 = persons.stream()
.filter(p -> App.getAge(p.getBirthDate()) >= 18)
.collect(Collectors.toList());
//App.printList(filteredList1);
// 2-Map (param: Function)
Function<String, String> coderFunction = name -> "Coder " + name;
List<String> filteredList2 = persons.stream()
//.filter(p -> App.getAge(p.getBirthDate()) >= 18)
//.map(p -> App.getAge(p.getBirthDate()))
//.map(p -> "Coder " + p.getName())
//.map(p-> p.getName())
.map(Person::getName)
.map(coderFunction).toList();
//App.printList(filteredList2);
// 3-Sorted (param: Comparator)
Comparator<Person> byNameAsc = (Person o1, Person o2) -> o1.getName().compareTo(o2.getName());
Comparator<Person> byNameDesc = (Person o1, Person o2) -> o2.getName().compareTo(o1.getName());
Comparator<Person> byBirthDate = (Person o1, Person o2) -> o1.getBirthDate().compareTo(o2.getBirthDate());
List<Person> filteredList3 = persons.stream()
.sorted(byBirthDate)
.collect(Collectors.toList());
//App.printList(filteredList3);
// 4-Match (param: Predicate)
Predicate<Person> startsWithPredicate = person -> person.getName().startsWith("J");
// anyMatch : No evalua todo el stream, termina en la coincidencia
boolean rpta1 = persons.stream()
.anyMatch(startsWithPredicate);
// allMatch : Evalua todo el stream bajo la condicion
boolean rpta2 = persons.stream()
.allMatch(startsWithPredicate);
// noneMatch : Evalua todo el stream bajo la condicion
boolean rpta3 = persons.stream()
.noneMatch(startsWithPredicate);
// 5-Limit/Skip
int pageNumber = 1;
int pageSize = 2;
List<Person> filteredList4 = persons.stream()
.skip(pageNumber * pageSize)
.limit(pageSize)
.collect(Collectors.toList());
//App.printList(filteredList4);
// 6-Collectors
// GroupBy
Map<String, List<Product>> collect1 = products.stream()
.filter(p -> p.getPrice() > 20)
.collect(Collectors.groupingBy(Product::getName));
//System.out.println(collect1);
// Counting
Map<String, Long> collect2 = products.stream()
.collect(Collectors.groupingBy(
Product::getName, Collectors.counting()
)
);
//System.out.println(collect2);
//Agrupando por nombre producto y sumando
Map<String, Double> collect3 = products.stream()
.collect(Collectors.groupingBy(
Product::getName,
Collectors.summingDouble(Product::getPrice)
)
);
//System.out.println(collect3);
//Obteniendo suma y resumen
DoubleSummaryStatistics statistics = products.stream()
.collect(Collectors.summarizingDouble(Product::getPrice));
//System.out.println(statistics);
//7-reduce
Optional<Double> sum = products.stream()
.map(Product::getPrice)
.reduce(Double::sum);
//.reduce((a,b) -> a+b)
//System.out.println(sum.get());
}
public static int getAge(LocalDate birthDate) {
return Period.between(birthDate, LocalDate.now()).getYears();
}
public static void printList(List<?> list){
list.forEach(System.out::println);
}
}