CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-js

Kotlin Standard Library for JavaScript target with comprehensive W3C DOM bindings and JavaScript interoperability features.

Pending
Overview
Eval results
Files

collections.mddocs/

Collections

JavaScript-optimized implementations of Kotlin collection interfaces with specialized performance characteristics for JavaScript runtime. These collections provide mutable data structures with efficient operations adapted for JavaScript environments.

Capabilities

Mutable List Implementation

Resizable array implementation optimized for JavaScript runtime.

/**
 * Resizable array implementation of MutableList interface
 */
actual class ArrayList<E> : AbstractMutableList<E>, RandomAccess {
    constructor()
    constructor(initialCapacity: Int)
    constructor(elements: Collection<E>)
    
    actual override val size: Int
    actual override fun get(index: Int): E
    actual override fun set(index: Int, element: E): E
    actual override fun add(element: E): Boolean
    actual override fun add(index: Int, element: E)
    actual override fun remove(element: E): Boolean
    actual override fun removeAt(index: Int): E
    actual override fun clear()
    actual override fun indexOf(element: E): Int
    actual override fun lastIndexOf(element: E): Int
    actual override fun iterator(): MutableIterator<E>
    actual override fun listIterator(): MutableListIterator<E>
    actual override fun listIterator(index: Int): MutableListIterator<E>
    actual override fun subList(fromIndex: Int, toIndex: Int): MutableList<E>
}

Usage Examples:

// Create ArrayList
val list = ArrayList<String>()
val prefilledList = arrayListOf("apple", "banana", "cherry")

// Add elements
list.add("first")
list.add(0, "prepended") // Insert at specific position

// Access elements
val first = list[0]
val size = list.size

// Modify elements
list[1] = "modified"
list.removeAt(0)
list.remove("specific")

// Iteration
for (item in list) {
    println(item)
}

// Conversion
val regularList: List<String> = list

Mutable Map Implementations

Hash table and linked hash map implementations for key-value storage.

/**
 * Hash table implementation of MutableMap interface
 */
actual class HashMap<K, V> : AbstractMutableMap<K, V> {
    constructor()
    constructor(initialCapacity: Int)
    constructor(original: Map<out K, V>)
    
    actual override val size: Int
    actual override val keys: MutableSet<K>
    actual override val values: MutableCollection<V>
    actual override val entries: MutableSet<MutableMap.MutableEntry<K, V>>
    actual override fun get(key: K): V?
    actual override fun put(key: K, value: V): V?
    actual override fun remove(key: K): V?
    actual override fun clear()
    actual override fun containsKey(key: K): Boolean
    actual override fun containsValue(value: V): Boolean
}

/**
 * Insertion-ordered hash table implementation of MutableMap interface
 */
actual class LinkedHashMap<K, V> : HashMap<K, V> {
    constructor()
    constructor(initialCapacity: Int)
    constructor(original: Map<out K, V>)
    
    // Maintains insertion order for iteration
    actual override val keys: MutableSet<K>
    actual override val values: MutableCollection<V>
    actual override val entries: MutableSet<MutableMap.MutableEntry<K, V>>
}

Usage Examples:

// Create HashMap
val map = HashMap<String, Int>()
val prefilledMap = hashMapOf("one" to 1, "two" to 2)

// Add entries
map["key"] = 42
map.put("another", 100)

// Access entries
val value = map["key"] // 42
val containsKey = map.containsKey("key") // true
val containsValue = map.containsValue(42) // true

// Iteration (unordered)
for ((key, value) in map) {
    println("$key -> $value")
}

// LinkedHashMap preserves insertion order
val linkedMap = LinkedHashMap<String, String>()
linkedMap["first"] = "A"
linkedMap["second"] = "B"
linkedMap["third"] = "C"

// Iteration maintains insertion order
for ((key, value) in linkedMap) {
    println("$key -> $value") // Always: first->A, second->B, third->C
}

Mutable Set Implementations

Hash set and linked hash set implementations for unique element storage.

/**
 * Hash set implementation of MutableSet interface
 */
actual class HashSet<E> : AbstractMutableSet<E> {
    constructor()
    constructor(initialCapacity: Int)
    constructor(elements: Collection<E>)
    
    actual override val size: Int
    actual override fun add(element: E): Boolean
    actual override fun remove(element: E): Boolean
    actual override fun clear()
    actual override fun contains(element: E): Boolean
    actual override fun iterator(): MutableIterator<E>
}

/**
 * Insertion-ordered hash set implementation of MutableSet interface
 */
actual class LinkedHashSet<E> : HashSet<E> {
    constructor()
    constructor(initialCapacity: Int)
    constructor(elements: Collection<E>)
    
    // Maintains insertion order for iteration
    actual override fun iterator(): MutableIterator<E>
}

Usage Examples:

// Create HashSet
val set = HashSet<String>()
val prefilledSet = hashSetOf("red", "green", "blue")

// Add elements
set.add("unique")
set.add("unique") // Duplicate ignored

// Check membership
val contains = set.contains("unique") // true
val size = set.size

// Remove elements
set.remove("unique")
set.clear()

// LinkedHashSet preserves insertion order
val linkedSet = LinkedHashSet<Int>()
linkedSet.add(3)
linkedSet.add(1) 
linkedSet.add(2)

// Iteration maintains insertion order
for (item in linkedSet) {
    println(item) // Always: 3, 1, 2
}

Abstract Base Classes

Base implementations providing common functionality for mutable collections.

/**
 * Abstract base class for mutable collections
 */
abstract class AbstractMutableCollection<E> : AbstractCollection<E>, MutableCollection<E> {
    abstract override fun add(element: E): Boolean
    abstract override fun remove(element: E): Boolean
    abstract override fun iterator(): MutableIterator<E>
    
    override fun clear() // Default implementation
    override fun addAll(elements: Collection<E>): Boolean // Default implementation
    override fun removeAll(elements: Collection<E>): Boolean // Default implementation
    override fun retainAll(elements: Collection<E>): Boolean // Default implementation
}

/**
 * Abstract base class for mutable lists
 */
abstract class AbstractMutableList<E> : AbstractMutableCollection<E>, MutableList<E> {
    abstract override fun get(index: Int): E
    abstract override fun set(index: Int, element: E): E
    abstract override fun add(index: Int, element: E)
    abstract override fun removeAt(index: Int): E
    
    override fun indexOf(element: E): Int // Default implementation
    override fun lastIndexOf(element: E): Int // Default implementation
    override fun listIterator(): MutableListIterator<E> // Default implementation
    override fun listIterator(index: Int): MutableListIterator<E> // Default implementation
    override fun subList(fromIndex: Int, toIndex: Int): MutableList<E> // Default implementation
}

/**
 * Abstract base class for mutable maps
 */
abstract class AbstractMutableMap<K, V> : AbstractMap<K, V>, MutableMap<K, V> {
    abstract override val entries: MutableSet<MutableMap.MutableEntry<K, V>>
    
    override fun put(key: K, value: V): V? // Default implementation
    override fun remove(key: K): V? // Default implementation
    override fun clear() // Default implementation
    override fun putAll(from: Map<out K, V>) // Default implementation
    override val keys: MutableSet<K> // Default implementation
    override val values: MutableCollection<V> // Default implementation
}

/**
 * Abstract base class for mutable sets
 */
abstract class AbstractMutableSet<E> : AbstractMutableCollection<E>, MutableSet<E> {
    // Inherits from AbstractMutableCollection
    // No additional abstract methods required
}

String-Optimized Collections

Specialized collection implementations optimized for String keys and elements.

/**
 * Create a string-optimized mutable map
 */
fun <V> stringMapOf(): MutableMap<String, V>
fun <V> stringMapOf(vararg pairs: Pair<String, V>): MutableMap<String, V>

/**
 * Create a string-optimized mutable set
 */
fun stringSetOf(): MutableSet<String>
fun stringSetOf(vararg elements: String): MutableSet<String>

/**
 * Create a linked string-optimized mutable map (preserves insertion order)
 */
fun <V> linkedStringMapOf(): MutableMap<String, V>
fun <V> linkedStringMapOf(vararg pairs: Pair<String, V>): MutableMap<String, V>

/**
 * Create a linked string-optimized mutable set (preserves insertion order)
 */
fun linkedStringSetOf(): MutableSet<String>
fun linkedStringSetOf(vararg elements: String): MutableSet<String>

Usage Examples:

// String-optimized collections (better performance for string operations)
val stringMap = stringMapOf(
    "name" to "John",
    "city" to "New York",
    "country" to "USA"
)

val stringSet = stringSetOf("apple", "banana", "cherry")

// Linked versions preserve insertion order
val linkedStringMap = linkedStringMapOf(
    "first" to "A",
    "second" to "B", 
    "third" to "C"
)

val linkedStringSet = linkedStringSetOf("red", "green", "blue")

// Use like regular collections
stringMap["age"] = "30"
stringSet.add("date")

// Iteration order is preserved for linked versions
for ((key, value) in linkedStringMap) {
    println("$key: $value") // Consistent order
}

Collection Factory Functions

Convenient factory functions for creating pre-populated collections.

/**
 * Create ArrayList with initial elements
 */
fun <T> arrayListOf(): ArrayList<T>
fun <T> arrayListOf(vararg elements: T): ArrayList<T>

/**
 * Create HashMap with initial entries
 */  
fun <K, V> hashMapOf(): HashMap<K, V>
fun <K, V> hashMapOf(vararg pairs: Pair<K, V>): HashMap<K, V>

/**
 * Create HashSet with initial elements
 */
fun <T> hashSetOf(): HashSet<T>
fun <T> hashSetOf(vararg elements: T): HashSet<T>

/**
 * Create LinkedHashMap with initial entries (preserves insertion order)
 */
fun <K, V> linkedMapOf(): LinkedHashMap<K, V>
fun <K, V> linkedMapOf(vararg pairs: Pair<K, V>): LinkedHashMap<K, V>

/**
 * Create LinkedHashSet with initial elements (preserves insertion order)
 */
fun <T> linkedSetOf(): LinkedHashSet<T>
fun <T> linkedSetOf(vararg elements: T): LinkedHashSet<T>

Usage Examples:

// Empty collections
val list = arrayListOf<String>()
val map = hashMapOf<String, Int>()
val set = hashSetOf<Int>()

// Pre-populated collections
val fruits = arrayListOf("apple", "banana", "cherry")
val scores = hashMapOf("Alice" to 95, "Bob" to 87, "Carol" to 92)
val numbers = hashSetOf(1, 2, 3, 4, 5)

// Linked collections (preserve insertion order)
val orderedMap = linkedMapOf("first" to 1, "second" to 2, "third" to 3)
val orderedSet = linkedSetOf("red", "green", "blue")

// Type inference works
val auto = arrayListOf(1, 2, 3) // ArrayList<Int>
val autoMap = hashMapOf("key" to 42) // HashMap<String, Int>

Types

// Concrete collection implementations
actual class ArrayList<E> : AbstractMutableList<E>, RandomAccess
actual class HashMap<K, V> : AbstractMutableMap<K, V>
actual class HashSet<E> : AbstractMutableSet<E>
actual class LinkedHashMap<K, V> : HashMap<K, V>
actual class LinkedHashSet<E> : HashSet<E>

// Abstract base classes
abstract class AbstractMutableCollection<E> : AbstractCollection<E>, MutableCollection<E>
abstract class AbstractMutableList<E> : AbstractMutableCollection<E>, MutableList<E>
abstract class AbstractMutableMap<K, V> : AbstractMap<K, V>, MutableMap<K, V>
abstract class AbstractMutableSet<E> : AbstractMutableCollection<E>, MutableSet<E>

// Marker interface for random access
interface RandomAccess

// Mutable collection interfaces (from kotlin.collections)
interface MutableCollection<E> : Collection<E>
interface MutableList<E> : List<E>, MutableCollection<E>
interface MutableSet<E> : Set<E>, MutableCollection<E>
interface MutableMap<K, V> : Map<K, V>

// Iterator interfaces
interface MutableIterator<T> : Iterator<T>
interface MutableListIterator<T> : ListIterator<T>, MutableIterator<T>

Install with Tessl CLI

npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-js

docs

browser-integration.md

collections.md

coroutines.md

index.md

io-encoding.md

javascript-interop.md

math-time.md

reflection.md

uuid.md

w3c-dom-apis.md

tile.json