Spec-Zone.ru › Kotlin 2

hashMapOf

kotlin-stdlib/kotlin.collections/hashMapOf

В Kotlin с: 1.1

inline fun <K, V> hashMapOf(): HashMap<K, V>

Возвращает новую пустую HashMap.

Примеры

import kotlin.test.*
import java.util.*

fun main() { 
   //sampleStart 
   val map = hashMapOf<Int, Any?>()
println("map.isEmpty() is ${map.isEmpty()}") // true

map[1] = "x"
map[2] = 1.05
// Now map contains something:
println(map) // {1=x, 2=1.05} 
   //sampleEnd
}

fun <K, V> hashMapOf(vararg pairs: Pair<K, V>): HashMap<K, V>

Возвращает новую HashMap с указанным содержимым в виде списка пар, где первый элемент — ключ, а второй — значение.

Примеры

import kotlin.test.*
import java.util.*

fun main() { 
   //sampleStart 
   val map: HashMap<Int, String> = hashMapOf(1 to "x", 2 to "y", -1 to "zz")
println(map) // {-1=zz, 1=x, 2=y} 
   //sampleEnd
}

© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/hash-map-of.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API