Skip to content

Instantly share code, notes, and snippets.

@theeasiestway
theeasiestway / convert_map_jni.cpp
Last active April 24, 2023 16:43
Java HashMap to C++ std::map conversion and vice versa
jobject StlStringStringMapToJavaHashMap(JNIEnv *env, const std::map<std::string, std::string>& map) {
jclass mapClass = env->FindClass("java/util/HashMap");
if(mapClass == NULL)
return NULL;
jmethodID init = env->GetMethodID(mapClass, "<init>", "()V");
jobject hashMap = env->NewObject(mapClass, init);
jmethodID put = env->GetMethodID(mapClass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
std::map<std::string, std::string>::const_iterator citr = map.begin();