Skip to content

Instantly share code, notes, and snippets.

View ch8n's full-sized avatar
💻
Always on to writing my next article.

Chetan Gupta ch8n

💻
Always on to writing my next article.
View GitHub Profile
@ch8n
ch8n / linkedHashMap_signature.kt
Created October 12, 2020 13:34
linkedHashMap signature in kotlin
// Kotlin signature
typealias LinkedHashMap<K, V> = LinkedHashMap<K, V>
// Java signature
class LinkedHashMap<K,V>
extends HashMap<K,V>
implements Map<K,V>
// Available Constructors
Java LinkedHashMap has three constructors
- LinkedHashMap()
- LinkedHashMap(int initialCapacity)
@ch8n
ch8n / map_to_hashmap.kt
Created October 12, 2020 13:30
map to hashmap in kotlin
// Directly use the 4th constructor example
val contact = mapOf("chetan" to "[email protected]")
val hashMapOfContact = HashMap(contact)
//Or create and add all entries
val hashMapOfContact = HashMap<String,Int>()
hashMapOfContact.putAll(contact)
@ch8n
ch8n / direct_hashmap.kt
Created October 12, 2020 13:28
directly create hashmap in kotlin
val hashMap = HashMap<String,Int>()
@ch8n
ch8n / hashMapOf_definations.kt
Created October 12, 2020 13:25
hashMapOf definations kotlin
fun <K, V> hashMapOf(vararg pairs: Pair<K, V>): HashMap<K, V>
= HashMap<K, V>(mapCapacity(pairs.size)).apply { putAll(pairs) }
// this factory function takes pair and uses HashMap's second constructor which
// start off with the object capacity as the number of pairs passed
@ch8n
ch8n / hashMap_kotlin_java.kt
Created October 12, 2020 13:24
hashMap kotlin java kotlin
fun <K, V> hashMapOf(): HashMap<K, V> = HashMap<K, V>()
// hashmapOf directly create HashMap object default constructor
// Java HashMap default constructor create HashMap with
// * inital capacity 16 and * LoadFactor 0.75
@ch8n
ch8n / hashmap_stdlib_functions.kt
Created October 12, 2020 13:22
hashmap stdlib functions kotlin
// Kotlin Standard Library function
fun <K, V> hashMapOf(): HashMap<K, V>
fun <K, V> hashMapOf(vararg pairs: Pair<K, V>): HashMap<K, V>
@ch8n
ch8n / hashmap_signature.kt
Created October 12, 2020 13:20
Hash map kotlin and java signature
// Kotlin signature
typealias HashMap<K, V> = HashMap<K, V>
// Java signature
class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
// AbstractMap<K,V> is a skeletal implementation of the Map interface,
// Java's Map<K,V> is very close to MutableMap<K,V> than kotlin's Map<K,V>
// i.e it contains mutable opertaions internally, though use structures named
// similar to Kotlin's Map<K,V> like Map.Entry but are infact Mutable Entry
fun <K, V> mutableMapOf(vararg pairs: Pair<K, V>): MutableMap<K, V>
= LinkedHashMap<K, V>(mapCapacity(pairs.size))
.apply { putAll(pairs) }
// resolved with LinkedHashMap only
@ch8n
ch8n / mutable_map_resolved_linkedhashMap.kt
Created October 12, 2020 13:17
mutablemap resolved by linkedhashMap
fun <K, V> mutableMapOf(): MutableMap<K, V> = LinkedHashMap()
// mutableMapOf is resolved using LinkedHashMap
// as discussed earlier Linked HashMap signature
class LinkedHashMap<K, V> : MutableMap<K, V>
@ch8n
ch8n / mutable_map_creation_stdlib_function.kt
Created October 12, 2020 13:16
mutablemap creation stdlib function
// Kotlin Standard Library function
fun <K, V> mutableMapOf(): MutableMap<K, V>
fun <K, V> mutableMapOf(vararg pairs: Pair<K, V> ): MutableMap<K, V>