Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I look up a Clojure keyword in IPersistentMap from Java?

Tags:

java

clojure

Calling a Clojure library from a Java program. I get an IPersistentMap back.

The keys are Clojure keywords like :name etc.

But it seems I can't just do a

map.valAt(":name");

to pull it out in Java. I'm guessing that's because the keys aren't normal java Strings. So what are they? And how can I pull data out of an IPersistentMap?

like image 567
interstar Avatar asked Nov 27 '14 07:11

interstar


2 Answers

You don't need to use the reader. Clojure keywords are of type clojure.lang.Keyword. You can create one using the static method intern:

map.valAt(clojure.lang.Keyword.intern("name")); // Note: no leading colon

like image 154
Nathan Davis Avatar answered Oct 10 '22 18:10

Nathan Davis


Have you tried using Clojure.read ? =>

map.valAt(Clojure.read(":name"))

or maybe => // (:name my-map) myMap.invoke(Clojure.read(":name"));

Clojure 1.6 Java API https://github.com/clojure/clojure/blob/master/changes.md#21-java-api

Clojure's JavaDoc https://clojure.github.io/clojure/javadoc/

like image 45
runexec Avatar answered Oct 10 '22 18:10

runexec