Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

java native interface - JNI: Create HashMap

How do I create a HashMap object in JNI?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is code, you will need to modify to work

jclass mapClass = (*env)->FindClass(env, "java/util/HashMap");
if(mapClass == NULL)
{
    return NULL;
}


jsize map_len = 1;

jmethodID init = (*env)->GetMethodID(env, mapClass, "<init>", "(I)V");
jobject hashMap = (*env)->NewObject(env, mapClass, init, map_len);

jmethodID put = (*env)->GetMethodID(env, mapClass, "put",
            "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");

while( ... )
{
    jint key = ...;
    size_t sz = t->count;
    jbyteArray dd = (*env)->NewByteArray(env, sz);
    for(i = 0; i < sz; i++)
    {
        (*env)->SetByteArrayRegion(env, dd, i, 1, *data++);
    }

    (*env)->CallObjectMethod(env, hashMap, put, key, dd);

    (*env)->DeleteLocalRef(env, key);
    (*env)->DeleteLocalRef(env, dd);
}

(*env)->DeleteLocalRef(env, hashMap);
(*env)->DeleteLocalRef(env, mapClass);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...