A Java Hashmap is not a Clojure collection

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com

I did not know this till just now. I have a co-worker who wrote a library in Java. From my Clojure code, I call this Java function:

    public HashMap<String,HashMap<String,Integer>> init(String debrief,String companyName, ArrayList<String> contacts, ArrayList<String> accounts, ArrayList<String> requiredFields) {
        try {
            return transformer.transform(debrief, companyName, tecClassifier, rollioClassifier, caseClassifier, caselessClassifier, customClassifier, pipeline, parser, props, firstNames, lastNames, accounts, contacts, requiredFields);
        } catch (Exception e) {
            System.out.println("Top level Exception in Main::main" + e.getMessage());
        return null;

I get back a Java Hashmap. If I do:

(let [sentence-parse-response-map (.init instance-of-sentence-parser incoming-message "Rollio" accounts contacts)

(clojure.pprint/pprint sentence-parse-response-map))

I see something in the terminal that looks like a Clojure map:

{“account_name” “hanson inc”
“close_date” “2015-11-12″}

But if I try:

(assoc sentence-parse-response-map “user-token” “00Di0000000kUIT!AQUAQDBzR9j4GSm9H3TgCKywiIik8tFdGe”)

I get:

java.util.HashMap cannot be cast to clojure.lang.Associative

On Stackoverflow, I found a simple solution for casting the Hashmap to a Clojure collection:

(assoc (into sentence-parse-response-map {}) “user-token” “00Di0000000kUIT!AQUAQDBzR9j4GSm9H3TgCKywiIik8tFdGe”)

That is a bit of ceremony that I dislike. When I use Clojure I enjoy freedom from ceremony. And I also worry that the next developer will see that and think “What the hell was he thinking? That “into” is useless so I am going to remove it.”

But it seems that any time we interact with Java, we must pay some price in terms of stupid ceremony.