简体   繁体   English

Kotlin:类型推断失败。 预期类型不匹配:推断类型为 MutableList<Long?> 但可变集合<Long>预料之中

[英]Kotlin: Type inference failed. Expected type mismatch: inferred type is MutableList<Long?> but MutableCollection<Long> was expected

I'm attempting to create a MutableList using kotlin but I'm getting an error stating:我正在尝试使用 kotlin 创建一个 MutableList 但我收到一条错误消息:

Type inference failed.类型推断失败。 Expected type mismatch: inferred type is MutableList but MutableCollection was expected预期类型不匹配:推断类型为 MutableList 但预期为 MutableCollection

...and I'm not sure how to convert the MutableList to a MutableCollection. ...而且我不确定如何将 MutableList 转换为 MutableCollection。

I've tried using:我试过使用:

.toMutableList().toCollection()

but it's looking for a destination - and I'm not sure what to do.但它正在寻找目的地——我不知道该怎么做。

Code Snippet:代码片段:

data class HrmSearchResult(
    var rssi: Short?,
    var adjustRssi: Short?,
    var timeout: Int,
    var serialNumber: Long?,
    var isIn: Boolean,
    var countIn: Int
)

private val hashMapHrm = ConcurrentHashMap<Long?, HrmSearchResult>()

val hrmDeviceList: MutableCollection<Long>
    get() = try {
        if (hashMapHrm.elements().toList().none { it.isIn}) {
            //if there are no member in range, then return empty list
            arrayListOf()
        } else {
            hashMapHrm.elements()
                .toList()
                .filter { it.isIn }
                .sortedByDescending { it.adjustRssi }
                .map { it.serialNumber }
                .toMutableList().toCollection()
        }
    } catch (ex: Exception) {
        AppLog.e(
            LOG, "Problem when get devices " +
                    "return empty list: ${ex.localizedMessage}"
        )
        arrayListOf()
    }

Any suggestions are appreciated.任何建议表示赞赏。

The problem is the nullability, not the collection type, ie that you are creating a List<Long?> where a List<Long> is expected.问题是可空性,而不是集合类型,即您正在创建一个List<Long?> ,其中需要List<Long>

You can reproduce your error message ( inferred type is MutableList<Long?> but MutableCollection<Long> was expected ) minimally with this:您可以使用以下方法最低限度地重现您的错误消息( inferred type is MutableList<Long?> but MutableCollection<Long> was expected ):

val foo: MutableCollection<Long> =
    listOf(1L, 2, 3, 4, null)
        .toMutableList()

And you can fix it by inserting .filterNotNull() to remove potential nulls, and convert a List<T?> to a List<T> :您可以通过插入.filterNotNull()来删除潜在的空值来修复它,并将List<T?>转换为List<T>

val foo: MutableCollection<Long> =
    listOf(1L, 2, 3, 4, null)
        .filterNotNull()
        .toMutableList()

(So your .toCollection() call is actually not needed and can be dropped) (所以你的.toCollection()调用实际上是不需要的,可以删除)

Some other notes specific to your code:特定于您的代码的其他一些注意事项:

You probably want to use .values over .elements.toList() , and map { }.filterNotNull() can be combined into mapNotNull , so in summary, you probably want to write your chain as您可能希望在.elements.toList()上使用.values ,并且map { }.filterNotNull()可以组合成mapNotNull ,所以总而言之,您可能希望将链写为

hashMapHrm.values
    .filter { it.isIn }
    .sortedByDescending { it.adjustRssi }
    .mapNotNull { it.serialNumber }
    .toMutableList()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型不匹配:推断的类型是但预期的 - Type mismatch: inferred type is but was expected 类型推断失败。 预期的类型不匹配:响应 <Void> 发现延期 <Response<Void> &gt; - Type inference failed. Expected type mismatch: Response<Void> found Deferred<Response<Void>> 类型不匹配:推断类型是字符串? 但预计字符串 kotlin - Type mismatch: inferred type is String? but String was expected kotlin Android Kotlin - viewBinding 类型不匹配:推断的类型是 DrawerLayout 但 ConstraintLayout 是预期的 - Android Kotlin - viewBinding Type mismatch: inferred type is DrawerLayout but ConstraintLayout was expected Kotlin - 类型不匹配:推断的类型是 Unit,但 Intent 是预期的 - Kotlin - Type mismatch: inferred type is Unit but Intent was expected Kotlin类型不匹配:推断类型是View! 但TextView是预料之中的 - Kotlin Type mismatch: inferred type is View! but TextView was expected 类型不匹配:推断类型是String,但是在kotlin中预计会出现Charset - Type mismatch: inferred type is String but Charset was expected in kotlin 类型不匹配:推断的类型是上下文? 但希望有上下文-Kotlin - Type mismatch: inferred type is Context? but Context was expected - Kotlin Kotlin:类型不匹配:推断的类型是 Intent? 但意图是预期的 - Kotlin : Type mismatch: inferred type is Intent? but Intent was expected 类型不匹配:推断类型为 String 但预期为 Int,Kotlin - Type mismatch: inferred type is String but Int was expected , Kotlin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM