interstar
interstar

Reputation: 27186

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

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?

Upvotes: 6

Views: 728

Answers (2)

Nathan Davis
Nathan Davis

Reputation: 5766

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

Upvotes: 9

runexec
runexec

Reputation: 862

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/

Upvotes: 5

Related Questions