- Notifications
You must be signed in to change notification settings - Fork0
A bimap (bidirectional map) implementation for Kotlin
License
uchuhimo/kotlinx-bimap
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A bimap (bidirectional map) implementation for Kotlin.
- JDK 1.6 or higher
This library provides interfaces for read-only/mutable bimap:
Interface | Bases | Implementations |
---|---|---|
BiMap | Map | emptyBiMap ,biMapOf ,toBiMap |
MutableBiMap | MutableMap ,BiMap | mutableBiMapOf ,toMutableBiMap |
Create an empty read-only bimap:
val newBiMap= emptyBiMap()
Create a new read-only bimap from pairs:
val newBiMap= biMapOf(1 to"1",2 to"2",3 to"3")
Create a new read-only bimap from map:
val newBiMap=mapOf(1 to"1",2 to"2",3 to"3").toBiMap()
Create an empty mutable bimap:
val newBiMap= mutableBiMapOf()
Create a new mutable bimap from pairs:
val newBiMap= mutableBiMapOf(1 to"1",2 to"2",3 to"3")
Create a new mutable bimap from map:
val newBiMap=mapOf(1 to"1",2 to"2",3 to"3").toMutableBiMap()
Bimap support all operations of map:
val biMap= biMapOf(1 to"1",2 to"2",3 to"3")biMap.size// 3biMap.isEmpty()// falsebiMap.values// ["1", "2", "3"]biMap[1]// "1"biMap.containsKey(4)// false
Get the inverse view of bimap:
val biMap= biMapOf(1 to"1",2 to"2",3 to"3")val inverseBiMap= biMap.inverseinverseBiMap.values// [1, 2, 3]
Mutable bimap support all operations of mutable map:
val mutableBiMap= mutableBiMapOf(1 to"1",2 to"2",3 to"3")mutableBiMap[3]="4"mutableBiMap.remove(1)mutableBiMap.clear()
When usingput
operation (i.e.,mutableBiMap[3] = "4"
), the bimap throwsIllegalArgumentException
if the given value is already bound to a different key in it. The bimap will remain unmodified in this event. To avoid this exception, callforcePut
instead:
mutableBiMap.forcePut(4,"2")
TheforcePut
operation will silently remove any existing entry with the value before proceeding with theput
operation.
Views a mutable bimap as a Guava bimap:
val mutableBiMap= mutableBiMapOf(1 to"1",2 to"2",3 to"3")val guavaBiMap= mutableBiMap.asGuavaBiMap()
Views a Guava bimap as a mutable bimap:
val guavaBiMap=HashBiMap.create(mapOf(1 to"1",2 to"2",3 to"3"))val mutableBiMap= guavaBiMap.asMutableBiMap()
This library has been published toMaven Central,JCenter andJitPack.
<dependency> <groupId>com.uchuhimo</groupId> <artifactId>kotlinx-bimap</artifactId> <version>1.2</version></dependency>
compile'com.uchuhimo:kotlinx-bimap:1.2'
compile(group="com.uchuhimo", name="kotlinx-bimap", version="1.2")
Build library with Gradle using the following command:
gradlew clean assemble
Test library with Gradle using the following command:
gradlew clean test
Since Gradle has excellent incremental build support, you can usually omit executing theclean
task.
Install library in a local Maven repository for consumption in other projects via the following command:
gradlew clean install
© uchuhimo, 2017-2018. Licensed under anApache 2.0 license.
About
A bimap (bidirectional map) implementation for Kotlin