JNI: Create HashMap

后端 未结 5 775
失恋的感觉
失恋的感觉 2020-12-15 23:55

How do I create a HashMap object in JNI?

5条回答
  •  [愿得一人]
    2020-12-16 00:08

    One may also consider alternatives to directly using JNI - e.g. tools that can generate JNI code for you. For example, JANET (disclaimer: I wrote it) allows you to embed Java code in your native methods, so creating and using a hash map is then as simple as:

    ... (C++ code)
    `Map map = new HashMap();` // embedded Java
    ... (C++ code)
    ... const char* foo = "foo";
    `map.put(#$(foo), 50);` // ["foo" -> 50]
    

    the back-ticked statements get translated by JANET to JNI code, so you never have to worry about signatures, reference handling, exception handling, etc. yet you still get the performance of JNI.

提交回复
热议问题