Skip to content

Instantly share code, notes, and snippets.

@wellingtonpgp
Created July 1, 2020 02:59
Show Gist options
  • Save wellingtonpgp/f0457f153e642557743d73cf2aa0f42d to your computer and use it in GitHub Desktop.
Save wellingtonpgp/f0457f153e642557743d73cf2aa0f42d to your computer and use it in GitHub Desktop.
public class _Stream {
public static void main(String[] args) {
List<Person> people = List.of(
new Person("Jonh", MALE),
new Person("Maria", FEMALE),
new Person("Aisha", FEMALE),
new Person("Alex", MALE),
new Person("Alice", FEMALE),
new Person("Bob", PREFER_NOT_TO_SAY)
);
// people.stream()
// .map(person -> person.name)
// .collect(Collectors.toSet())
// .forEach(System.out::println);
//
// people.stream()
// .map(person -> person.name)
// .mapToInt(String::length)
// .forEach(System.out::println);
//
// boolean containsOnlyFemales = people.stream()
// .allMatch(person -> FEMALE.equals(person.gender));
//
// System.out.println(containsOnlyFemales);
String dto1 = null;
String dto2 = "";
people.stream()
.filter(dto1 != null ? e -> e.name.equals("Jonh") : e -> true)
.filter(dto2 != null ? person -> FEMALE.equals(person.gender) : person -> true)
.collect(Collectors.toList()).forEach(System.out::println);
}
static class Person {
private final String name;
private final Gender gender;
public Person(String name, Gender gender) {
this.name = name;
this.gender = gender;
}
@Override
public String toString() {
return "Person {name=" + name + ", gender=" + gender + "}";
}
}
enum Gender {
MALE, FEMALE, PREFER_NOT_TO_SAY
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment