|
| 1 | +import java.time.LocalDate; |
| 2 | +import java.util.*; |
| 3 | +import java.util.function.Function; |
| 4 | +import java.util.function.Supplier; |
| 5 | + |
| 6 | +public class Josegs95 { |
| 7 | + public static void main(String[] args) { |
| 8 | + //Ejercicio |
| 9 | + //Las funciones de orden superior están muy relacionadas en java con todo el contenido del paquete |
| 10 | + //java.util.function y las expresiones lambda, ambos introducidos en la versión 8 de Java. |
| 11 | + |
| 12 | + //Pasar una función como parámetro |
| 13 | + int a = 8; |
| 14 | + int result = operate(a, x -> x * x); |
| 15 | + System.out.println(result); |
| 16 | + |
| 17 | + //Devolver una función desde otra función |
| 18 | + int b = 3; |
| 19 | + Supplier<Integer> supplier = doubleValue(b); |
| 20 | + System.out.println(supplier.get()); |
| 21 | + |
| 22 | + //Reto |
| 23 | + retoFinal(); |
| 24 | + } |
| 25 | + |
| 26 | + public static int operate(int n, Function<Integer, Integer> function){ |
| 27 | + return function.apply(n); |
| 28 | + } |
| 29 | + |
| 30 | + public static Supplier<Integer> doubleValue(int n){ |
| 31 | + return () -> n * 2; |
| 32 | + } |
| 33 | + |
| 34 | + public static void retoFinal(){ |
| 35 | + List<Student> studentList = new ArrayList<>(); |
| 36 | + studentList.add(new Student("Jose", |
| 37 | + LocalDate.of(1995, 2, 28), |
| 38 | + Arrays.asList(10.0, 8.25, 9.25, 10.0))); |
| 39 | + studentList.add(new Student("María", |
| 40 | + LocalDate.of(1997, 4, 13), |
| 41 | + Arrays.asList(6.5, 6.0, 8.5, 8.0))); |
| 42 | + studentList.add(new Student("Guillermo", |
| 43 | + LocalDate.of(1992, 7, 22), |
| 44 | + Arrays.asList(9.0, 9.5, 7.75, 8.5))); |
| 45 | + |
| 46 | + //Promedio calificaciones |
| 47 | + List<String> averageMarks = studentList.stream() |
| 48 | + .map(s-> { |
| 49 | + double average = s.getMarkList() |
| 50 | + .stream() |
| 51 | + .mapToDouble(Double::doubleValue) |
| 52 | + .average() |
| 53 | + .getAsDouble(); |
| 54 | + return s.getName() + ":" + average; |
| 55 | + }).toList(); |
| 56 | + System.out.println("Nota media de los estudiantes: " + averageMarks); |
| 57 | + |
| 58 | + //Mejores estudiantes (+9 de nota) |
| 59 | + List<String> bestStudents = averageMarks.stream() |
| 60 | + .filter(s -> { |
| 61 | + double averageMark = Double.parseDouble(s.substring(s.indexOf(":") + 1)); |
| 62 | + return averageMark > 9.0; |
| 63 | + }).toList(); |
| 64 | + System.out.println("Estudiantes con mas de un 9 de nota media: " + bestStudents); |
| 65 | + |
| 66 | + //Lista de estudiantes ordenada por nacimiento |
| 67 | + List<Student> sortedStudentList = studentList.stream() |
| 68 | + .sorted((x, y) -> x.getBirthday().compareTo(y.getBirthday()) * -1) |
| 69 | + .toList(); |
| 70 | + System.out.println("Estudiantes ordenados ascendentemente por edad: " + sortedStudentList); |
| 71 | + |
| 72 | + //Mejor calificación de los estudiantes |
| 73 | + double bestMark = studentList.stream() |
| 74 | + .mapToDouble(s -> s.getMarkList() |
| 75 | + .stream() |
| 76 | + .max(Double::compareTo).get()) |
| 77 | + .max().getAsDouble(); |
| 78 | + System.out.println("Nota mas alta de entre los estudiantes: " + bestMark); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + public static class Student { |
| 83 | + private String name; |
| 84 | + private LocalDate birthday; |
| 85 | + private List<Double> markList; |
| 86 | + |
| 87 | + public Student(String name, LocalDate birthday, List<Double> markList) { |
| 88 | + this.name = name; |
| 89 | + this.birthday = birthday; |
| 90 | + this.markList = markList; |
| 91 | + } |
| 92 | + |
| 93 | + public String getName() { |
| 94 | + return name; |
| 95 | + } |
| 96 | + |
| 97 | + public void setName(String name) { |
| 98 | + this.name = name; |
| 99 | + } |
| 100 | + |
| 101 | + public LocalDate getBirthday() { |
| 102 | + return birthday; |
| 103 | + } |
| 104 | + |
| 105 | + public void setBirthday(LocalDate birthday) { |
| 106 | + this.birthday = birthday; |
| 107 | + } |
| 108 | + |
| 109 | + public List<Double> getMarkList() { |
| 110 | + return markList; |
| 111 | + } |
| 112 | + |
| 113 | + public void setMarkList(List<Double> markList) { |
| 114 | + this.markList = markList; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public String toString() { |
| 119 | + return getName(); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments