forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookstore.java
More file actions
33 lines (26 loc) · 872 Bytes
/
Copy pathBookstore.java
File metadata and controls
33 lines (26 loc) · 872 Bytes
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
package modern.challenge;
import java.util.List;
import java.util.Optional;
import static java.util.stream.Collectors.toList;
public class Bookstore {
// Avoid
public List<Book> fetchBooksAvoid(List<String> isbns) {
return isbns.stream()
.map(this::fetchBookByIsbn)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(toList());
}
// Prefer
public List<Book> fetchBooksPrefer(List<String> isbns) {
return isbns.stream()
.map(this::fetchBookByIsbn)
.flatMap(Optional::stream)
.collect(toList());
}
public Optional<Book> fetchBookByIsbn(String isbn) {
// fetching book by the given "isbn" can return null
Book book = null;
return Optional.ofNullable(book);
}
}