Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

Key:
    Key : Value
    Key : Value
    Key : Value
Key :
    Key : Value
    Key : Value
    Key : Value
like image 959
Gustavo Vollbrecht Avatar asked Feb 25 '18 19:02

Gustavo Vollbrecht


People also ask

How similar is swift to Kotlin?

Kotlin is a programming language for Android app development, and Swift is for IOS application development. Both kotlin vs swift languages are built on top of the modern programming approach and software design pattern. Both the languages offer several inbuilt functions defined in an extensive list of libraries.

How do you store string in Kotlin?

String elements and templates –Elements store in a string from index 0 to (string. length – 1). Using index: Returns the character at specified index. Using get function: Returns the character at specified index passed as argument to get function.

How do you use string on Kotlin?

String values must be surrounded by double quotes (" ") or triple quote (""" """). We have two kinds of string available in Kotlin - one is called Escaped String and another is called Raw String. Escaped string is declared within double quote (" ") and may contain escape characters like '\n', '\t', '\b' etc.

How do I create an array string in Kotlin?

To create a String Array in Kotlin, use arrayOf() function. arrayOf() function creates an array of specified type and given elements.


1 Answers

This structure can be represented by a Map<String, Map<String, Any>>. The Kotlin code for creating such a 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, as of JDK 9, it can be expressed like this:

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 is basically the same as Object in Java.

like image 140
s1m0nw1 Avatar answered Sep 23 '22 11:09

s1m0nw1