I am new in Kotlin. I can't understand why Map DB is not working for me with kotlin. I tried google but it was no help.
gradle
dependencies {
compile(kotlin("stdlib-jdk8"))
implementation(group="org.mapdb", name= "mapdb", version= "3.0.7")
testCompile("junit", "junit", "4.12")
}
File.kt
import org.mapdb.DBMaker
fun main(array: Array<String>) {
val db = DBMaker.memoryDB().make()
val map = db.hashMap("map").createOrOpen()
map.put("a", "a")
db.close()
}
Error:(7, 13) Kotlin: Type mismatch: inferred type is String but Nothing? was expected. Projected type HTreeMap restricts use of public open fun put(key: K?, value: V?): V? defined in org.mapdb.HTreeMap
Error:(7, 18) Kotlin: Type mismatch: inferred type is String but Nothing? was expected. Projected type HTreeMap restricts use of public open fun put(key: K?, value: V?): V? defined in org.mapdb.HTreeMap
But this works with Java.
public static void main(String[] args) {
DB db = DBMaker.fileDB("java.db").fileMmapEnable().transactionEnable().make();
ConcurrentMap map = db.hashMap("map").createOrOpen();
map.put("a", "b");
map.put("a2", "b");
System.out.println(map);
System.out.println(map.getClass());
db.commit();
db.close();
DB db2 = DBMaker.fileDB("java.db").fileMmapEnable().transactionEnable().make();
ConcurrentMap map2 = db2.hashMap("map").open();
System.out.println(map2);
map2.forEach((o, o2) -> {
System.out.println(o+" = "+o2);
});
}
@gidds is completely right about that Kotlin doesn't allow "raw" Java types and requires type parameters. So you can just cast your map like this and it will work fine.
If you don't like unchecked casts as I do, you can use a bit more verbose HashMapMaker constructor like this.