Java / Kotlin equivalent to Swift [String: [String: Any]]

What is the equivalent of [String : [String : Any]] from Swift in Kotlin or Java?

I need to get a structure from the database that looks like this:

 Key: Key : Value Key : Value Key : Value Key : Key : Value Key : Value Key : Value 
+7
java android dictionary swift kotlin
source share
1 answer

This structure can be represented by Map<String, Map<String, Any>> . Kotlin's code for creating this type:

 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") ) 

In Java, starting with JDK 9, this can be expressed as follows:

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

Note that the Any type in Kotlin basically matches the Object in Java.

+13
source share

All Articles