Best practice from Rust's HashMap to Java Map? #495
-
A |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I wrote a utility for OpenDAL's usage: fn hashmap_to_jmap<'a>(env: &mut JNIEnv<'a>, map: &HashMap<String, String>) -> Result<JObject<'a>> {
let map_object = env.new_object("java/util/HashMap", "()V", &[])?;
let jmap = env.get_map(&map_object)?;
for (k, v) in map {
let key = env.new_string(k)?;
let value = env.new_string(v)?;
jmap.put(env, &key, &value)?;
}
Ok(map_object)
} |
Beta Was this translation helpful? Give feedback.
-
Converting between Java and Rust types in general is another story altogether. The way you've done it pretty much is the best practice at this point. Perhaps someone will write some conversion traits and/or a JNI binding generator in the future, but that would be a lot of work. |
Beta Was this translation helpful? Give feedback.
JNIEnv::new_string
is a wrapper around a C JNI function; it's built into the JVM.Converting between Java and Rust types in general is another story altogether. The way you've done it pretty much is the best practice at this point. Perhaps someone will write some conversion traits and/or a JNI binding generator in the future, but that would be a lot of work.