tk.hongkailiu.test.app.java8.lambda.Person.PersonUtil Maven / Gradle / Ivy
The newest version!
package tk.hongkailiu.test.app.java8.lambda.Person;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* Created by hongkailiu on 2015-04-25.
*/
public class PersonUtil {
public static void printPersons(List roster, CheckPerson tester) {
for (Person p : roster) {
if (tester.test(p)) {
p.printPerson();
}
}
}
public static void printPersonsWithPredicate(List roster, Predicate tester) {
for (Person p : roster) {
if (tester.test(p)) {
p.printPerson();
}
}
}
public static void processPersons(List roster, Predicate tester,
Consumer block) {
for (Person p : roster) {
if (tester.test(p)) {
block.accept(p);
}
}
}
public static void processPersonsWithFunction(List roster, Predicate tester,
Function mapper, Consumer block) {
for (Person p : roster) {
if (tester.test(p)) {
String data = mapper.apply(p);
block.accept(data);
}
}
}
public static void processElements(Iterable source, Predicate tester,
Function mapper, Consumer block) {
for (X p : source) {
if (tester.test(p)) {
Y data = mapper.apply(p);
block.accept(data);
}
}
}
public static void processElements(List roster) {
roster.stream()
.filter(p -> p.gender == Person.Sex.MALE && p.getAge() >= 18 && p.getAge() <= 25)
.map(p -> p.emailAddress).forEach(email -> System.out.println(email));
}
public static int compareByAge(Person a, Person b) {
if (a.birthday == null || b.birthday == null) {
throw new IllegalArgumentException("Field birthday is not initialized!");
}
return a.birthday.compareTo(b.birthday);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy