diff --git a/.settings/org.eclipse.jdt.apt.core.prefs b/.settings/org.eclipse.jdt.apt.core.prefs new file mode 100644 index 0000000..d4313d4 --- /dev/null +++ b/.settings/org.eclipse.jdt.apt.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.apt.aptEnabled=false diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..f9bf4ae --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,9 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=9 +org.eclipse.jdt.core.compiler.compliance=9 +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.processAnnotations=disabled +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=9 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 843c2bd..5457382 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,10 @@ -import java.util.ArrayList; + +//import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.stream.Collector; import java.util.stream.Collectors; public class Main { @@ -13,90 +15,70 @@ public static void main(String[] args) { // Imperative approach ❌ /* - - List females = new ArrayList<>(); - - for (Person person : people) { - - if (person.getGender().equals(Gender.FEMALE)) { - females.add(person); - } - } - - females.forEach(System.out::println); - - */ + * List females = new ArrayList<>(); + * + * for (Person person : people) { + * + * if (person.getGender().equals(Gender.FEMALE)) { females.add(person); } } + * + * females.forEach(System.out::println); + */ // Declarative approach ✅ - // Filter - List females = people.stream() - .filter(person -> person.getGender().equals(Gender.FEMALE)) + List females = people.stream().filter(person -> person.getGender().equals(Gender.FEMALE)) .collect(Collectors.toList()); -// females.forEach(System.out::println); + females.forEach(System.out::println); // Sort - List sorted = people.stream() - .sorted(Comparator.comparing(Person::getAge).thenComparing(Person::getGender).reversed()) - .collect(Collectors.toList()); + List sorted = people.stream().sorted(Comparator.comparing(Person::getAge)).collect(Collectors.toList()); -// sorted.forEach(System.out::println); + // sorted.forEach(System.out::println); // All match - boolean allMatch = people.stream() - .allMatch(person -> person.getAge() > 8); + boolean allMatch = people.stream().allMatch(person -> person.getAge() > 8); + System.out.println(allMatch); -// System.out.println(allMatch); // Any match - boolean anyMatch = people.stream() - .anyMatch(person -> person.getAge() > 121); + boolean anyMatch = people.stream().anyMatch(person -> person.getAge() > 8); + System.out.println(anyMatch); -// System.out.println(anyMatch); // None match - boolean noneMatch = people.stream() - .noneMatch(person -> person.getName().equals("Antonio")); - -// System.out.println(noneMatch); + boolean noneMatch = people.stream().noneMatch(person -> person.getName().equals("Fulanito")); + System.out.println(noneMatch); // Max - people.stream() - .max(Comparator.comparing(Person::getAge)); -// .ifPresent(System.out::println); + // Optional max = + people.stream().max(Comparator.comparing(Person::getAge)).ifPresent(person -> { + System.out.println(person); + }); + // System.out.println(max); // Min - people.stream() - .min(Comparator.comparing(Person::getAge)); -// .ifPresent(System.out::println); + people.stream().min(Comparator.comparing(Person::getAge)).ifPresent(System.out::println); // Group - Map> groupByGender = people.stream() - .collect(Collectors.groupingBy(Person::getGender)); + Map> groupoByGender = people.stream().collect(Collectors.groupingBy(Person::getGender)); + // System.out.println(groupoByGender); + groupoByGender.forEach((gender, people1) -> { + System.out.println(gender); + people1.forEach(System.out::println); + System.out.println(); + }); -// groupByGender.forEach((gender, people1) -> { -// System.out.println(gender); -// people1.forEach(System.out::println); -// System.out.println(); -// }); + Optional mujerMasMAyor = people.stream().filter(person -> person.getGender().equals(Gender.FEMALE)) + .max(Comparator.comparing(Person::getAge)).map(Person::getName); - Optional oldestFemaleAge = people.stream() - .filter(person -> person.getGender().equals(Gender.FEMALE)) - .max(Comparator.comparing(Person::getAge)) - .map(Person::getName); + mujerMasMAyor.ifPresent(System.out::println); - oldestFemaleAge.ifPresent(System.out::println); } private static List getPeople() { - return List.of( - new Person("Antonio", 20, Gender.MALE), - new Person("Alina Smith", 33, Gender.FEMALE), - new Person("Helen White", 57, Gender.FEMALE), - new Person("Alex Boz", 14, Gender.MALE), - new Person("Jamie Goa", 99, Gender.MALE), - new Person("Anna Cook", 7, Gender.FEMALE), - new Person("Zelda Brown", 120, Gender.FEMALE) - ); + return List.of(new Person("Antonio", 20, Gender.MALE), new Person("Alina Smith", 33, Gender.FEMALE), + new Person("Helen White", 57, Gender.FEMALE), new Person("Alex Boz", 14, Gender.MALE), + new Person("Jamie Goa", 99, Gender.MALE), new Person("Anna Cook", 7, Gender.FEMALE), + new Person("Zelda Brown", 120, Gender.FEMALE)); } }