Java Reference
In-Depth Information
return
birthday
;
}
public
LocalDate
getBirthdayFor
(
String
name
)
{
return
birthdays
.
get
(
name
);
}
public
int
getAgeInYear
(
String
name
,
int
year
)
{
Period
period
=
Period
.
between
(
birthdays
.
get
(
name
),
birthdays
.
get
(
name
).
withYear
(
year
));
return
period
.
getYears
();
}
public
Set
<
String
>
getFriendsOfAgeIn
(
int
age
,
int
year
)
{
return
birthdays
.
keySet
().
stream
()
.
filter
(
p
->
getAgeInYear
(
p
,
year
)
==
age
)
.
collect
(
Collectors
.
toSet
());
}
public
int
getDaysUntilBirthday
(
String
name
)
{
Period
period
=
Period
.
between
(
LocalDate
.
now
(),
birthdays
.
get
(
name
));
return
period
.
getDays
();
}
public
Set
<
String
>
getBirthdaysIn
(
Month
month
)
{
return
birthdays
.
entrySet
().
stream
()
.
filter
(
p
->
p
.
getValue
().
getMonth
()
==
month
)
.
map
(
p
->
p
.
getKey
())
.
collect
(
Collectors
.
toSet
());
}
public
Set
<
String
>
getBirthdaysInNextMonth
()
{
return
getBirthdaysIn
(
LocalDate
.
now
().
getMonth
());
}
public
int
getTotalAgeInYears
()
{
return
birthdays
.
keySet
().
stream
()
.
mapToInt
(
p
->
getAgeInYear
(
p
,
LocalDate
.
now
().
getYear
()))
.
sum
();
}
}
This class shows how to use the low-level API to build up useful functionality. It
also uses innovations such as the Java Streams API, and demonstrates how to use
LocalDate
as an immutable class and how dates should be treated as values.
m
m
n
s
a