secret
Last active

  • Download Gist
Member.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
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;
}
}
Roster.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
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);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.