record
Book
(String author, String title) {
String
getTitle() {
return this.title;
}
String
getAuthor() {
return this.author;
}
};
public
class ToMapEx {
public static void main(String[] args) {
List<Book> books = Arrays.asList(
new Book("Stephen King", "The Shining"),
new Book("Stephen King", "It"),
new Book("J.K. Rowling", "Harry Potter")
);
Map<String, String> authorMap = books.stream()
.collect(Collectors.toMap(
Book::getAuthor, // Key Mapper
Book::getTitle, // Value
Mapper
(v1, v2) -> v1 + " & " + v2 // Merge
Function
));
System.out.println(authorMap);
// {J.K. Rowling=Harry Potter, Stephen King=The Shining &
It}
}
}
{J.K. Rowling=Harry Potter, Stephen King=The Shining & It}