2017-03-03 69 views
0

任何人都知道我怎麼可以「把/放」在Kotlin的內部地圖?如何獲取/放Kotlin內部地圖或地圖的地圖

var bookDetailsMap: MutableMap<Long, MutableMap<Long, Book>> = mutableMapOf() 

我怎麼能「放」的書中細節到地圖上面

var books: MutableList<Book> = this.bookDao.findAll() 

認爲存儲會像>

var bookDetailsMap: MutableMap<book.publisherId, MutableMap<book.bookId, Book>> 

回答

0

成功做出這樣說:

放置示例:

var bookDetailsMap : Map<Long, Map<Long, BookDetail>> = hashMapOf() 

this.bookDetailsMap = this.bookDao.findAll().groupBy { 
    it.publisherId 
}.mapValues { 
    it.value.associateBy { it.bookId} 
} 

獲取例:

var bookDetail : BookDetail = BookDetail() 

if (this.bookDetailsMap .containsKey(this.publisherId)) { 
    bookDetail = this.bookDetailsMap [this.publisherId]!![this.bookId]!! 
}