Created
April 26, 2012 15:51
-
-
Save MasseGuillaume/2500529 to your computer and use it in GitHub Desktop.
Insert key value pair in a std::map then output in a java HashMap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @author Guillaume Masse | |
#include "JHashMap.h" | |
#include <string> | |
using namespace std; | |
void JHashMap::insert( std::string key, std::string value ) | |
{ | |
map.insert( pair<string,string>( key, value ) ); | |
} | |
jobject JHashMap::toJObject( JNIEnv* env, jclass ) | |
{ | |
jclass clazz = env->FindClass("java/util/HashMap"); | |
jmethodID init = env->GetMethodID(clazz, "<init>", "()V"); | |
jobject propertyMap = env->NewObject(clazz, init); | |
jmethodID putMethod = env->GetMethodID( | |
clazz, | |
"put", | |
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;" | |
); | |
for( auto it = map.begin(); it != map.end(); ++it ) | |
{ | |
jstring key = (*env).NewStringUTF( (*it).first.c_str() ); | |
jstring value = (*env).NewStringUTF( (*it).second.c_str() ); | |
(*env).CallVoidMethod( | |
propertyMap, | |
putMethod, | |
key, | |
value | |
); | |
} | |
return propertyMap; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
// @author Guillaume Masse | |
#include <map> | |
#include <string> | |
#include "jni.h" | |
class JHashMap | |
{ | |
public: | |
void insert( std::string key, std::string value ); | |
jobject toJObject(JNIEnv *, jclass); | |
private: | |
std::map< std::string, std::string > map; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, figured it out. Just removed jclass parameter :)