Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何转换列表<;Obj1>;映射<;Obj1.prop,列表<;Obj1.otherProp>;_Java_Java 8_Java Stream - Fatal编程技术网

Java 如何转换列表<;Obj1>;映射<;Obj1.prop,列表<;Obj1.otherProp>;

Java 如何转换列表<;Obj1>;映射<;Obj1.prop,列表<;Obj1.otherProp>;,java,java-8,java-stream,Java,Java 8,Java Stream,如何使用流将列表转换为列表的映射 我想将列表转换为地图, 不仅列表到映射 这是我要转换的列表: List<Book> bookList = Arrays.asList( new Book(new Author("first 1", "last 1"), "book 1 - 1"), new Book(new Author("first 1", "last 1"), "book 1 - 2"), new Book(new Author("

如何使用流将列表转换为列表的映射

我想将
列表
转换为
地图

不仅
列表
映射

这是我要转换的列表:

List<Book> bookList = Arrays.asList(
        new Book(new Author("first 1", "last 1"), "book 1 - 1"),
        new Book(new Author("first 1", "last 1"), "book 1 - 2"),
        new Book(new Author("first 2", "last 2"), "book 2 - 1"),
        new Book(new Author("first 2", "last 2"), "book 2 - 2")
);
List bookList=Arrays.asList(
新书(新作者(“第一本”、“最后一本”)、“第1-1本”),
新书(新作者(“第一本”、“最后一本”)、“第1-2本”),
新书(新作者(“前2本”、“后2本”)、“第2-1本”),
新书(新作者(“前2本”、“后2本”),“第2-2本”)
);
我知道怎么做:

// Map<Author.firstname, List<Book>> map = ...
Map<String, List<Book>> map = bookList.stream()
    .collect(Collectors.groupingBy(book -> book.getAuthor().getFirstName()));
//映射=。。。
Map Map=bookList.stream()
.collect(Collectors.groupingBy(book->book.getAuthor().getFirstName());
但我能做些什么来获得它:

// Map<Author.firstname, List<Book.title>> map2 = ...
Map<String, List<String>> map2 = new HashMap<String, List<String>>() {
    {
        put("first 1", new ArrayList<String>() {{
            add("book 1 - 1");
            add("book 1 - 2");
        }});
        put("first 2", new ArrayList<String>() {{
            add("book 2 - 1");
            add("book 2 - 2");
        }});
    }
}; 

// Map<Author.firstname, List<Book.title>> map2 = ...
Map<String, Map<String, List<String>> map2 = bookList.stream(). ...
                                                                ^^^
//映射映射2=。。。
Map map2=新的HashMap(){
{
put(“第一个1”,新ArrayList(){{
添加(“第1-1册”);
添加(“第1-2册”);
}});
put(“前2”,新ArrayList(){{
添加(“第2-1册”);
添加(“第2-2册”);
}});
}
}; 
//映射map2=。。。

映射使用
收集器。映射
将每本
书籍
映射到其相应的标题:

Map<String, List<String>> map = bookList.stream()
    .collect(Collectors.groupingBy(book -> book.getAuthor().getFirstName(),
                                   Collectors.mapping(Book::getTitle,Collectors.toList())));
Map Map=bookList.stream()
.collect(Collectors.groupingBy(book->book.getAuthor().getFirstName(),
Collectors.mapping(Book::getTitle,Collectors.toList());
Map<String, List<String>> map = bookList.stream()
    .collect(Collectors.groupingBy(book -> book.getAuthor().getFirstName(),
                                   Collectors.mapping(Book::getTitle,Collectors.toList())));