Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
package java8.snippets.socialnet;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Member {
public enum Sex {
MALE, FEMALE
}
String name;
Calendar birthday;
Sex gender;
String emailAddress;
Member(String nameArg, Calendar birthdayArg,
Sex genderArg, String emailArg) {
name = nameArg;
birthday = birthdayArg;
gender = genderArg;
emailAddress = emailArg;
}
public int getAge() {
Calendar today = GregorianCalendar.getInstance();
int age = today.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
if((birthday.get(Calendar.MONTH) > today.get(Calendar.MONTH))
|| (birthday.get(Calendar.MONTH) == today.get(Calendar.MONTH)
&& birthday.get(Calendar.DAY_OF_MONTH)
> today.get(Calendar.DAY_OF_MONTH)))
{
age--;
}
return age;
}
public void printMember() {
System.out.println(name + ", " + this.getAge());
}
public Sex getGender() {
return gender;
}
public String getName() {
return name;
}
public String getEmailAddress() {
return emailAddress;
}
}
package java8.snippets.socialnet;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
public class Roster {
interface CheckMember {
boolean test(Member p);
}
interface Block<T> {
void apply(T a);
}
public static void printMembersOlderThan(List<Member> roster, int age) {
for (Member p : roster) {
if (p.getAge() >= age) {
p.printMember();
}
}
}
public static void printMembersWithinAgeRange(
List<Member> roster, int low, int high) {
for (Member p : roster) {
if (low <= p.getAge() && p.getAge() > high) {
p.printMember();
}
}
}
public static void printMembers(
List<Member> roster, CheckMember tester) {
for (Member p : roster) {
if (tester.test(p)) {
p.printMember();
}
}
}
public static void processMembers(
List<Member> roster,
CheckMember tester,
Block<Member> block) {
for (Member p : roster) {
if (tester.test(p)) {
block.apply(p);
}
}
}
public static void processMembersWithMapper(
List<Member> roster,
Predicate<Member> tester,
Function<Member,String> mapper,
Consumer<String> block) {
for (Member p : roster) {
if (tester.test(p)) {
String data = mapper.apply(p);
block.accept(data);
}
}
}
public static <X,Y> void processElements(
List<X> roster,
Predicate<X> tester,
Function<X,Y> mapper,
Consumer<Y> block) {
for (X x : roster) {
if (tester.test(x)) {
Y data = mapper.apply(x);
block.accept(data);
}
}
}
public static void main(String... args) {
List<Member> roster = new ArrayList<>();
roster.add(
new Member(
"Fred",
new GregorianCalendar(1980, 6, 20),
Member.Sex.MALE,
"fred@example.com"));
roster.add(
new Member(
"Jane",
new GregorianCalendar(1990, 7, 15),
Member.Sex.FEMALE, "jane@example.com"));
roster.add(
new Member(
"George",
new GregorianCalendar(1991, 8, 13),
Member.Sex.MALE, "george@example.com"));
roster.add(
new Member(
"Bob",
new GregorianCalendar(2000, 9, 12),
Member.Sex.MALE, "bob@example.com"));
for (Member p : roster) {
p.printMember();
}
System.out.println("Members older than 20:");
printMembersOlderThan(roster, 20);
System.out.println();
System.out.println("Members between the ages of 14 and 30:");
printMembersWithinAgeRange(roster, 14, 30);
System.out.println();
printMembers(roster, new CheckMember() {
@Override
public boolean test(Member p) {
return p.getGender() == Member.Sex.MALE
&& p.getAge() > 20;
}
});
processMembers(roster, new CheckMember() {
@Override
public boolean test(Member p) {
return p.getGender() == Member.Sex.MALE;
}
}, new Block<Member>() {
@Override
public void apply(Member a) {
a.printMember();
}
});
System.out.println("############# Here!!! PLEASE");
processMembersWithMapper(
roster,
m -> m.getGender() == Member.Sex.FEMALE,
Member::getEmailAddress,
System.out::println);
processElements(
roster,
m -> m.getGender() == Member.Sex.FEMALE,
m -> m.getEmailAddress(),
email -> System.out.println(email)
);
roster.stream().parallel()
.filter(m -> m.getGender() == Member.Sex.FEMALE)
.map(Member::getEmailAddress)
.forEach(System.out::println);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.