Java/Kotlin 等效于 Swift [字符串 : [String : Any]]

标签 java android swift dictionary kotlin

在 Kotlin 或 Java 语言中,Swift 中的 [String : [String : Any]] 是什么?

我需要从数据库中检索如下结构:

Key:
    Key : Value
    Key : Value
    Key : Value
Key :
    Key : Value
    Key : Value
    Key : Value

最佳答案

这个结构可以用 Map<String, Map<String, Any>> 表示.创建此类类型的 Kotlin 代码:

val fromDb: Map<String, Map<String, Any>> = mapOf(
    "Key1" to mapOf("KeyA" to "Value", "KeyB" to "Value"),
    "Key2" to mapOf("KeyC" to "Value", "KeyD" to "Value")
)

在 Java 中,从 JDK 9 开始,可以这样表示:

Map<String, Map<String, Object>> fromDb = Map.of(
    "Key1", Map.of("KeyA", "Value", "KeyB", "Value"),
    "Key2", Map.of("KeyC", "Value", "KeyD", "Value")
);

请注意 Any Kotlin 中的 type 与 Object 基本相同在 Java 中。

关于Java/Kotlin 等效于 Swift [字符串 : [String : Any]],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48977450/

相关文章:

objective-c - NSOutlineView 和 isItemExpandable 在 Swift 中不起作用

ios - 使用 Swift 更新 Core Data 中的多对多关系

java - Facebook 删除请求

java - 添加复选框后 Android 应用程序崩溃

java - Tomcat 8 |如何为连接器使用自定义 key 管理器

Android - 使用 ParseFacebookUtils.logIn 时 Facebook session 关闭

android - 在其他位置显示上下文操作栏

android - 当我向 ListView 项目添加 View 时,该项目不再正确 react

string - 获取有关 Swift 中搜索栏的信息

java - Maven : No compile errors but still cant find symbol