pub struct JMap<'local, 'other_local_1: 'obj_ref, 'obj_ref> { /* private fields */ }
Expand description
Wrapper for JObjects that implement java/util/Map
. Provides methods to get
and set entries and a way to iterate over key/value pairs.
Looks up the class and method ids on creation rather than for every method call.
Implementations§
source§impl<'local, 'other_local_1: 'obj_ref, 'obj_ref> JMap<'local, 'other_local_1, 'obj_ref>
impl<'local, 'other_local_1: 'obj_ref, 'obj_ref> JMap<'local, 'other_local_1, 'obj_ref>
sourcepub fn from_env(
env: &mut JNIEnv<'local>,
obj: &'obj_ref JObject<'other_local_1>
) -> Result<JMap<'local, 'other_local_1, 'obj_ref>>
pub fn from_env( env: &mut JNIEnv<'local>, obj: &'obj_ref JObject<'other_local_1> ) -> Result<JMap<'local, 'other_local_1, 'obj_ref>>
Create a map from the environment and an object. This looks up the necessary class and method ids to call all of the methods on it so that exra work doesn’t need to be done on every method call.
sourcepub fn get<'other_local_2>(
&self,
env: &mut JNIEnv<'other_local_2>,
key: &JObject<'_>
) -> Result<Option<JObject<'other_local_2>>>
pub fn get<'other_local_2>( &self, env: &mut JNIEnv<'other_local_2>, key: &JObject<'_> ) -> Result<Option<JObject<'other_local_2>>>
Look up the value for a key. Returns Some
if it’s found and None
if
a null pointer would be returned.
sourcepub fn put<'other_local_2>(
&self,
env: &mut JNIEnv<'other_local_2>,
key: &JObject<'_>,
value: &JObject<'_>
) -> Result<Option<JObject<'other_local_2>>>
pub fn put<'other_local_2>( &self, env: &mut JNIEnv<'other_local_2>, key: &JObject<'_>, value: &JObject<'_> ) -> Result<Option<JObject<'other_local_2>>>
Look up the value for a key. Returns Some
with the old value if the
key already existed and None
if it’s a new key.
sourcepub fn remove<'other_local_2>(
&self,
env: &mut JNIEnv<'other_local_2>,
key: &JObject<'_>
) -> Result<Option<JObject<'other_local_2>>>
pub fn remove<'other_local_2>( &self, env: &mut JNIEnv<'other_local_2>, key: &JObject<'_> ) -> Result<Option<JObject<'other_local_2>>>
Remove a value from the map. Returns Some
with the removed value and
None
if there was no value for the key.
sourcepub fn iter<'map, 'iter_local>(
&'map self,
env: &mut JNIEnv<'iter_local>
) -> Result<JMapIter<'map, 'local, 'other_local_1, 'obj_ref, 'iter_local>>
pub fn iter<'map, 'iter_local>( &'map self, env: &mut JNIEnv<'iter_local> ) -> Result<JMapIter<'map, 'local, 'other_local_1, 'obj_ref, 'iter_local>>
Get key/value iterator for the map. This is done by getting the
EntrySet
from java and iterating over it.
The returned iterator does not implement std::iter::Iterator
and
cannot be used with a for
loop. This is because its next
method
uses a &mut JNIEnv
to call the Java iterator. Use a while let
loop
instead:
let mut iterator = map.iter(env)?;
while let Some((key, value)) = iterator.next(env)? {
let key: AutoLocal<JObject> = env.auto_local(key);
let value: AutoLocal<JObject> = env.auto_local(value);
// Do something with `key` and `value` here.
}
Each call to next
creates two new local references. To prevent
excessive memory usage or overflow error, the local references should
be deleted using JNIEnv::delete_local_ref
or JNIEnv::auto_local
before the next loop iteration. Alternatively, if the map is known to
have a small, predictable size, the loop could be wrapped in
JNIEnv::with_local_frame
to delete all of the local references at
once.