Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR prueba #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .settings/org.eclipse.jdt.apt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false
9 changes: 9 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
100 changes: 41 additions & 59 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -13,90 +15,70 @@ public static void main(String[] args) {
// Imperative approach ❌

/*

List<Person> females = new ArrayList<>();

for (Person person : people) {

if (person.getGender().equals(Gender.FEMALE)) {
females.add(person);
}
}

females.forEach(System.out::println);

*/
* List<Person> 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<Person> females = people.stream()
.filter(person -> person.getGender().equals(Gender.FEMALE))
List<Person> 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<Person> sorted = people.stream()
.sorted(Comparator.comparing(Person::getAge).thenComparing(Person::getGender).reversed())
.collect(Collectors.toList());
List<Person> 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<Person> 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<Gender, List<Person>> groupByGender = people.stream()
.collect(Collectors.groupingBy(Person::getGender));
Map<Gender, List<Person>> 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<String> mujerMasMAyor = people.stream().filter(person -> person.getGender().equals(Gender.FEMALE))
.max(Comparator.comparing(Person::getAge)).map(Person::getName);

Optional<String> 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<Person> 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));
}

}