Skip to content

Commit 5748911

Browse files
committed
Another solution by Maurice Naftalin.
1 parent abbda48 commit 5748911

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/org/jcrete/lambdas/solutions/exercise7/Database.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
*/
2222
package org.jcrete.lambdas.solutions.exercise7;
2323

24-
import java.util.*;
24+
import java.util.ArrayList;
25+
import java.util.Arrays;
26+
import java.util.Comparator;
27+
import java.util.List;
2528
import java.util.function.Function;
2629
import java.util.stream.Collectors;
2730

@@ -41,14 +44,14 @@ public class Database {
4144
public static void main(String[] args) {
4245
// TODO: find all the books which have the word "Java" in the title
4346
List<Book> books = BOOK_DB.stream()
44-
.filter(book -> book.getTitle().contains("Java"))
45-
.collect(Collectors.toList());
47+
.filter(book -> book.getTitle().contains("Java"))
48+
.collect(Collectors.toList());
4649
print(books, "Books which have the word \"Program\" in the title: ");
4750
// TODO: Find the titles of books whose author's name is "Bloch"
4851
List<String> titles = BOOK_DB.stream()
49-
.filter(book -> book.getAuthors().stream().anyMatch(s -> s.contains("Bloch")))
50-
.map(Book::getTitle)
51-
.collect(Collectors.toList());
52+
.filter(book -> book.getAuthors().stream().anyMatch(s -> s.contains("Bloch")))
53+
.map(Book::getTitle)
54+
.collect(Collectors.toList());
5255
// List<String> titles = BOOK_DB.stream().flatMap(book -> book.getAuthors().stream().filter(author -> author.equals("Bloch, Joshua"))).map(b -> b.getTitle()).collect(Collectors.toList());
5356
print(titles, "Titles of books whose author's name is Bloch: ");
5457
// TODO: find the names of all authors who have written at least two books
@@ -58,7 +61,7 @@ public static void main(String[] args) {
5861
.entrySet().stream()
5962
.filter(e -> e.getValue().size() > 1)
6063
.map(e -> e.getKey())
61-
.collect(Collectors.<String>toList());
64+
.collect(Collectors.toList());
6265
// Solution contributed by Marc Hoffmann; no need for getFirstAuthor() method
6366
// List<String> authors = BOOK_DB.stream()
6467
// .flatMap(book -> book.getAuthors().stream())
@@ -67,17 +70,26 @@ public static void main(String[] args) {
6770
// .filter(e -> e.getValue().size() > 1)
6871
// .map(e -> e.getKey())
6972
// .collect(Collectors.<String>toList());
73+
// Another Solution contributed by Maurice Naftalin
74+
// System.out.println("Names of all authors who have written at least 2 books: ");
75+
// BOOK_DB.stream()
76+
// .flatMap(book -> book.getAuthors().stream())
77+
// .collect(Collectors.toMap(i -> i, a -> 1, (b, c) -> b + c))
78+
// .entrySet().stream()
79+
// .filter(e -> e.getValue() > 1)
80+
// .map(e -> e.getKey())
81+
// .forEach(System.out::println);
7082
print(authors, "Names of all authors who have written at least 2 books: ");
7183
// TODO: sort by author, then by title
7284
List<Book> bookDBCopy = new ArrayList<>(BOOK_DB);
7385
// one way
7486
Function<Book, String> fByAuthor = book -> book.getAuthors().get(0);
75-
Comparator<Book> byAuthor = Comparator.comparing(fByAuthor);
87+
Comparator<Book> byAuthor = Comparator.comparing(fByAuthor);
7688
// Comparator<Book> byAuthor = Comparator.comparing((Book book) -> book.getAuthors().get(0));
7789
// another way
7890
// Function<Book, String> fByTitle = book -> book.getTitle();
7991
// Comparator<Book> byTitle = Comparator.comparing(fByTitle);
80-
Comparator<Book> byTitle = Comparator.comparing((Book book) -> book.getTitle());
92+
Comparator<Book> byTitle = Comparator.comparing(Book::getTitle);
8193
bookDBCopy.sort(byAuthor.thenComparing(byTitle));
8294
print(bookDBCopy, "Sorted by author, then by title");
8395

0 commit comments

Comments
 (0)