简体   繁体   English

将HashMap转换为Clojure映射的行为与Java调用不同

[英]Converting a HashMap into Clojure map performs not as excepted from a java call

I'm very new to Clojure. 我对Clojure非常陌生。 I want to pass a List<HashMap<String,String>> to a Clojure function in which I want to use the inner HashMaps as regular Clojure maps but I cannot get values from the map with using the :key function (which works fine for a regular Clojure map) I use the into {} function but it does not make what I except it to make. 我想将List<HashMap<String,String>>传递给Clojure函数,在其中我想将内部HashMaps用作常规Clojure映射,但是我无法使用:key函数从映射中获取值(对于常规的Clojure映射),但我使用了into {}函数,但除生成外,它没有其他功能。 What is what I do wrong? 我做错了什么? (Note: This is a demostration test code just to see the behaviour) (注意:这是一个演示测试代码,仅用于查看行为)

Java code: Java代码:

package com.experimental.clojure.java;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.experimental.clojure.test.ConvertTest;

public class Test_Cloj_Convert {
    public static void main(String[] args) {
        List<Map<String, String>> columns = new ArrayList<Map<String, String>>();
        Map<String, String> col1 = new HashMap<String, String>();
        col1.put("name", "ID");
        col1.put("type", "int");
        col1.put("pos", "0");
        Map<String, String> col2 = new HashMap<String, String>();
        col2.put("name", "Name");
        col2.put("type", "string");
        col2.put("pos", "2");
        Map<String, String> col3 = new HashMap<String, String>();
        col3.put("name", "Description");
        col3.put("type", "enum");
        col3.put("pos", "1");
        columns.add(col1);
        columns.add(col2);
        columns.add(col3);

        ConvertTest scripter = new ConvertTest();
        System.out.println(scripter.conv(columns));
    }
}

Clojure code Clojure代码

(ns com.experimental.clojure.test.ConvertTest
  (:gen-class
   :name com.experimental.clojure.test.ConvertTest
   :methods [
             [conv [java.util.List] String]
  ])
  (:import [java.util List] [java.util HashMap])  
)

(defn conv
    [columns]
    (println columns)
    (println (first columns))
    (println (:type (first columns)))
    (println (into {} (first columns)))
    (println (:type (into {} (first columns))))
)

(defn -conv
  [this columns]
  (conv columns)
)

And the (suprising) output 和(令人惊讶的)输出

#<ArrayList [{name=ID, type=int, pos=0}, {name=Name, type=string, pos=2}, {name=Description, type=enum, pos=1}]>
#<HashMap {name=ID, type=int, pos=0}>
nil
{name ID, type int, pos 0}
nil
null

What I've excepted for the third println the return the string "int". 除第三次println外,我返回的字符串是“ int”。 And in the fourth println it is obvious that the HashMap is not properly converted into Clojure map. 在第四个println中,很明显HashMap没有正确转换为Clojure映射。 Can you help what to do for the successful conversion? 您能帮忙成功转换吗? (I know I could use the get() function of the HashMap but it would be more comfortable to able to use it as a Clojure map) (我知道我可以使用HashMap的get()函数,但是将其用作Clojure映射会更舒适)

Two issues I see: 我看到两个问题:

  • Your conv function is returning nil, because println is the last statement and that returns nil 您的conv函数返回nil,因为println是最后一条语句,并且返回nil
  • ":type" is a Clojure keyword which is not equal to the String key "type" - hence the lookups are failing to find the value you want “:type”是Clojure关键字,它不等于字符串键“ type”-因此,查找无法找到所需的值

I understand now more and it was asking a silly thing. 我现在了解得更多,这是在问一个愚蠢的事情。 Sorry for all, but I was under the impression that in Clojure the following two structures are indentical. 抱歉,但我给人的印象是,在Clojure中,以下两个结构是相同的。 {"a" 1, "b" 2, "c" 3} and {:a 1, :b 2, :c 3} They are not and I tried to use the wrong method for getting the data from the map. {"a" 1, "b" 2, "c" 3}{:a 1, :b 2, :c 3}它们不是,我尝试使用错误的方法从地图中获取数据。 I tried with using (println ((into {} (first columns)) "pos")) and it works fine. 我尝试使用(println ((into {} (first columns)) "pos")) ,它工作正常。 I'm sorry for not realizing this sooner. 抱歉,您未及早意识到这一点。

However, if I may ask again. 但是,我是否可以再问一次。 What is the real difference between them? 它们之间的真正区别是什么? Right now I only have a vague idea about that. 现在,我对此只有一个模糊的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM