Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Example 2 from the Jan. 2016 "Programmez" article (fair use), with some minor formatting changes and some variable names translated to English:
- Old code:
- */
- PaymentCard mainCardToReturn = null;
- for(final PaymentCard card: paymentCards) {
- if(!card.isExpired() && !card.isDefault()
- && mainCardToReturn == null ||
- mainCardToReturn.creationDate().before(card.getCreationDate())) {
- mainCardToReturn = card;
- }
- }
- /*
- New Java-8 stream Code:
- */
- PaymentCard mainCardToReturn = paymentCards.stream()
- .filter(card -> !card.isExpired() && !card.isDefault())
- .min(Comparator.comparing(card -> card.getCreationDate().getTime()))
- .orElse(null);
- /*
- It's difficult to follow in the "Programmez" article, but apparently the benchmark for this change actually showed a performance improvement (executed in Contiperf 100 times, the old code took 30 ns and the new Java-8 stream code took 14 ns).
- What I do not understand is why payment cards flagged as "default" are filtered out, but that's the business logic they preserved.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement