|
| 1 | +import java.time.LocalDate; |
| 2 | +import java.util.Comparator; |
| 3 | +import java.util.Date; |
| 4 | +import java.util.List; |
| 5 | +import java.util.function.Function; |
| 6 | + |
| 7 | +public class danhingar { |
| 8 | + |
| 9 | + public static void main(String[] args) { |
| 10 | + System.err.println(applyFunc(String::length, "Daniel")); |
| 11 | + |
| 12 | + Function<Integer, Integer> number = applyMultiplier(5); |
| 13 | + int result = number.apply(5); |
| 14 | + System.out.println(result); |
| 15 | + |
| 16 | + List<Integer> numbers = List.of(1, 3, 4, 2, 5); |
| 17 | + System.out.println(numbers.stream().map(num -> num * 2).toList()); |
| 18 | + |
| 19 | + System.out.println(numbers.stream().filter(num -> num % 2 == 0).toList()); |
| 20 | + |
| 21 | + System.out.println(numbers.stream().sorted().toList()); |
| 22 | + |
| 23 | + System.out.println(numbers.stream().sorted(Comparator.reverseOrder()).toList()); |
| 24 | + |
| 25 | + System.out.println(numbers.stream().reduce(0, Integer::sum)); |
| 26 | + |
| 27 | + // EXTRA |
| 28 | + List<Student> students = List.of( |
| 29 | + new Student("PEPE", java.sql.Date.valueOf(LocalDate.parse("1987-04-29")), List.of(5.0, 8.5, 3.0, 10.0)), |
| 30 | + new Student("JUAN", java.sql.Date.valueOf(LocalDate.parse("1995-08-04")), List.of(1.0, 9.5, 2.0, 4.0)), |
| 31 | + new Student("LUIS", java.sql.Date.valueOf(LocalDate.parse("2000-12-15")), List.of(4.0, 6.5, 5.0, 2.0)), |
| 32 | + new Student("MATEO", java.sql.Date.valueOf(LocalDate.parse("1980-01-25")), List.of(10.0, 9.0, 9.7, 9.9))); |
| 33 | + |
| 34 | + //Promedio |
| 35 | + System.out.println(students.stream().map(s -> String.format("%s:%.2f", s.getName(), s.average())).toList()); |
| 36 | + |
| 37 | + //Mejores |
| 38 | + System.out.println(students.stream().filter(s-> s.average()>=9.0).map(Student::getName).toList()); |
| 39 | + |
| 40 | + //Fecha de nacimiento ordenada |
| 41 | + System.out.println(students.stream().sorted(Comparator.comparing(Student::getBirthdate, Comparator.nullsLast(Comparator.reverseOrder()))).toList()); |
| 42 | + |
| 43 | + //Califiación más alta |
| 44 | + System.out.println(students.stream().map(Student::getGrades).flatMap(List::stream).mapToDouble(Double::doubleValue).max().getAsDouble()); |
| 45 | + } |
| 46 | + |
| 47 | + public static <W, X> X applyFunc(Function<W, X> function, W cadena) { |
| 48 | + return function.apply(cadena); |
| 49 | + } |
| 50 | + |
| 51 | + public static Function<Integer, Integer> applyMultiplier(int n) { |
| 52 | + return (number) -> number * n; |
| 53 | + } |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +class Student { |
| 58 | + private String name; |
| 59 | + private Date birthdate; |
| 60 | + private List<Double> grades; |
| 61 | + |
| 62 | + public Student(String name, Date birthdate, List<Double> grades) { |
| 63 | + this.name = name; |
| 64 | + this.birthdate = birthdate; |
| 65 | + this.grades = grades; |
| 66 | + } |
| 67 | + |
| 68 | + public String getName() { |
| 69 | + return name; |
| 70 | + } |
| 71 | + |
| 72 | + public void setName(String name) { |
| 73 | + this.name = name; |
| 74 | + } |
| 75 | + |
| 76 | + public Date getBirthdate() { |
| 77 | + return birthdate; |
| 78 | + } |
| 79 | + |
| 80 | + public void setBirthdate(Date birthdate) { |
| 81 | + this.birthdate = birthdate; |
| 82 | + } |
| 83 | + |
| 84 | + public List<Double> getGrades() { |
| 85 | + return grades; |
| 86 | + } |
| 87 | + |
| 88 | + public void setGrades(List<Double> grades) { |
| 89 | + this.grades = grades; |
| 90 | + } |
| 91 | + |
| 92 | + public Double average() { |
| 93 | + return this.grades.stream().reduce(0.0, Double::sum) / this.grades.size(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public String toString() { |
| 98 | + return "Student [name=" + name + ", birthdate=" + birthdate + ", grades=" + grades + "]"; |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments