简体   繁体   English

Kotlin 中的并发哈希映射

[英]Concurrent Hash Map in Kotlin

Is it possible to implement a concurrent hash map purely in Kotlin (without Java dependency)?是否可以纯粹在 Kotlin 中实现并发哈希映射(不依赖 Java)? I am new to Kotlin and it looks like there is no obvious API available in kotlin.collections.我是 Kotlin 的新手,看起来 kotlin.collections 中没有明显的 API。

You can probably convert the source without too many issues.您可能可以在没有太多问题的情况下转换源代码。 It's freely available, here for example.它是免费提供的,例如在这里 The concurrency model of Kotlin multiplatform (which I'm guessing is your goal, there's no point in reimplementing it if you only target the JVM) is a bit different than the one Java uses, there are no locks for example. Kotlin 多平台的并发模型(我猜这是你的目标,如果你只针对 JVM,那么重新实现它是没有意义的)与 Java 使用的一种有点不同,例如没有锁。 But there's no reason why that would prevent it.但没有理由阻止它。

The following resources might also help you with the implementation:以下资源也可能对您的实施有所帮助:

You can try:你可以试试:

val emitters: ConcurrentMap<String, Any> = ConcurrentHashMap()

// get
  val obj: Any = emitters[email]

// put:
  emitters[email] = this

// delete
  emitters.remove(email)

Such way, u don't need to add any library to your project这样,你不需要在你的项目中添加任何库

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM