I have this simple Bean class:
public class Book {
public Book(Map<String, String> attribute) {
super();
this.attribute = attribute;
}
//key is isbn, val is author
private Map<String, String> attribute;
public Map<String, String> getAttribute() {
return attribute;
}
public void setAttribute(Map<String, String> attribute) {
this.attribute = attribute;
}
}
In my main class, I have added some information to the List:
Map<String, String> book1Details = new HashMap<String, String>();
book1Details.put("1234", "author1");
book1Details.put("5678", "author2");
Book book1 = new Book(book1Details);
Map<String, String> book2Details = new HashMap<String, String>();
book2Details.put("1234", "author2");
Book book2 = new Book(book2Details);
List<Book> books = new ArrayList<Book>();
books.add(book1);
books.add(book2);
Now I want to convert the books List to a map of this form:
Map<String, List<String>>
So that the output (the map above) is like:
//isbn: value1, value2
1234: author1, author2
5678: author1
So, I need to group the entries by isbn as key and authors as values. One isbn can have multiple authors.
I am trying like below:
Map<String, List<String>> library = books.stream().collect(Collectors.groupingBy(Book::getAttribute));
The format of the bean cannot be changed. If the bean had string values instead of map, I am able to do it, but stuck with the map.
I have written the traditional java 6/7 way of doing it correctly, but trying to do it via Java 8 new features. Appreciate the help.
See Question&Answers more detail:
os