- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
拥有一个以 List 作为值定义的 hashMap:
private var mMap: HashMap<String, List<DataStatus>>? = null
有一个函数返回一个 hashMap 但具有 MutableList 的值
fun getDataStatus(response: JSONObject?): HashMap<String, MutableList<DataStatus>> {
return HashMap<String, MutableList<AccountStatusAlert>>()
}
将结果传递给期望 List 的 hashMap 时出现错误:
mMap = getDataStatus(resp) //<== got error
出现错误:
Error:(81, 35) Type mismatch: inferred type is HashMap<String,
MutableList<DataStatus>> but HashMap<String, List<DataStatus>>? was expected
最佳答案
根据您的需要,您有两种解决方案。
转换它
考虑到 MutableList
是 List
的子类,您可以强制转换它。这里只有一个问题:您将失去不变性。如果将 List
转换回 MutableList
,则可以修改其内容。
mMap = getDataStatus(repo) as HashMap<String, List<String>>
转换它
为了保持列表的不变性,你必须将每个MutableList
转换成一个不可变的List
:
mMap = HashMap<String, List<String>>()
getDataStatus(repo).forEach { (s, list) ->
mMap?.put(s, list.toList())
}
在这种情况下,如果你试图在mMap
中修改列表的内容,将会抛出异常。
关于android - 在 kotlin 中,如何在目标需要 List 的地方传回 MutableList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46650079/
我想知道如何创建与给定newmatrix = MutableList>大小相同的matrix = MutableList>。特别是,我希望newmatrix为零,这可以通过执行循环来实现。 首先想到的
看起来很简单,但是,如何将 Kotlin 的 MutableList 初始化为空 MutableList? 我可以通过这种方式破解它,但我确信有一些更容易获得的东西: var pusta: List
我正在创建一个值列表,在这种情况下,尽管值一次添加一个,但最终数字是预先知道的。这是在一个将被多次调用的函数中,所以它运行得越快越好。 在 Java 中,我将使用指定初始容量的 ArrayList 构
我有一个mutableList。 var newList: MutableList = mutableListOf() 如何通过intent传递newList? 我尝试过,但没有成功。 mInten
我正在尝试编辑位于 MutableList 中的元素,但它们似乎是 val 类型。这是我尝试过的: for(note in item.notes.toMutableSet()) { note
scala> val x = mutable.MutableList[(Int, Int)]() x: scala.collection.mutable.MutableList[(Int, Int)]
我正在从特定文件夹获取文件路径列表,但是当我尝试将它添加到我的可变列表中时,该列表仍然是空的。 var filePath: MutableList? = null val path = File(ge
在 Kotlin 中,使用 listOf() 创建的列表功能 (which should be immutable)通过对 MutableList 的类型检查使用 is运算符。 示例: fun mai
Scala 的MutableList 有什么区别?和 ListBuffer scala.collection.mutable 中的类(class)?您什么时候使用一个与另一个? 我的用例是有一个线性序
该元素已经实现了深度复制。 fun f(a: MutableList>) { val copied = a.map { it.map { it.deepCopy() }.toMutableL
我问了一个问题 How to design a complex class which incude some classes to make expansion easily in future i
这是我的场景,我有几个 MutableList 成员,第一个用于存储 String (称为 sl),其余的用于 任何(称为对象,名称,...),我需要访问对象中的值,名称和其余的通过两种方式: sl
我正在尝试继承 MutableList,并向其中添加我自己的函数。例如: class CompositeJob : MutableList { fun cancelAllJobs() {
以下代码是我尝试将 RxJava 示例转换为 Kotlin 的示例。它应该收集一堆 Int变成了MutableList ,但我遇到了很多错误。 val all: Single> = Observabl
我有这个 token 对象: class Token(type: TokenType, value: String, position: IntRange = 0..0) 我声明一个 MutableL
我有一个列表,其中包含我从 api 中提取的数据。但是,我需要对此列表 (movieList) 进行更改。我需要将索引 0 处的元素与索引 1 处的元素交换。例如: list[0] = movieA,
我正在尝试使用 Jetpack Compose 在单击按钮时删除文本,但 UI 未更新。 我尝试设置一个按钮,然后单击一个按钮,deletedTasks 得到更新,如果我的任务在 deletedTas
我有一个 MutableList我想从中删除一个元素,但我找不到合适的方法。有一种方法可以从 ListBuffer 中删除元素像这样: val x = ListBuffer(1, 2, 3, 4, 5
请记住,我正在逐步进行函数式编程,呵呵。 我定义了这样一个可变列表: var list: MutableList? = null 因此,当我尝试使用list!!.add(E())时会抛出一个 kot
我对 Kotlin 还很陌生,目前我经常处理 JSON。 我从服务器收到一个 JSON 字符串,我将其解析为 JSONArray如下 var dataArray = JSONArray(String(
我是一名优秀的程序员,十分优秀!