I have a list (GlobalBooks) which looks similar to the below
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
GlobalBooks globalBooks = new GlobalBooks();
List<Book> bookList = new ArrayList<Book>();
Book book = new Book();
List<BookContent> bookContents = new ArrayList<BookContent>();
book.setBookName("A");
BookContent content = new BookContent();
content.setDescription("December 2016");
content.setComponentID(20l);
bookContents.add(content);
content = new BookContent();
content.setDescription("January 2016");
content.setComponentID(30l);
bookContents.add(content);
content = new BookContent();
content.setDescription("Febuary 2016");
content.setComponentID(40l);
bookContents.add(content);
book.setContents(bookContents);
bookList.add(book);
book = new Book();
bookContents = new ArrayList<BookContent>();
book.setBookName("B");
content = new BookContent();
content.setDescription("December 2016");
content.setComponentID(20l);
bookContents.add(content);
content = new BookContent();
content.setDescription("January 2016");
content.setComponentID(30l);
bookContents.add(content);
content = new BookContent();
content.setDescription("Febuary 2016");
content.setComponentID(40l);
bookContents.add(content);
book.setContents(bookContents);
bookList.add(book);
globalBooks.setBooks(bookList);
System.out.println(globalBooks);
}
}
I am looking for java 8 functions which could stream the globalBooks response and collect a map of List of Books based on the description field in the BooksContent.
Map<String,List<Books>> i.e. all the books with the same description should be grouped together by description ?
I can do this via regular java but the code becomes too messy and untidy.
MultiMap: Cleanest way to index a Collection by a property of the item's that itself is a collection