【发布时间】:2015-09-01 20:08:58
【问题描述】:
我有这个简单的 Bean 类:
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;
}
}
在我的主课中,我在列表中添加了一些信息:
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);
现在我想将书籍列表转换为这种形式的地图:
Map<String, List<String>>
所以输出(上图)是这样的:
//isbn: value1, value2
1234: author1, author2
5678: author1
因此,我需要按 isbn 作为键和作者作为值对条目进行分组。一个 isbn 可以有多个作者。
我正在尝试如下:
Map<String, List<String>> library = books.stream().collect(Collectors.groupingBy(Book::getAttribute));
bean 的格式无法更改。如果 bean 有字符串值而不是 map,我可以做到,但坚持使用 map。
我已经编写了正确的传统 java 6/7 方式,但尝试通过 Java 8 的新功能来实现。感谢您的帮助。
【问题讨论】:
标签: java collections lambda java-8