ninia / jep

Embed Python in Java
Other
1.31k stars 149 forks source link

implement keys() and items() on PyJMap #504

Closed ndjensen closed 11 months ago

ndjensen commented 11 months ago

Implementing keys() on PyJMap enables you to seamlessly convert a java.util.Map into a Python dictionary. In general this is unnecessary since PyJMap already supports common syntax used for dictionaries, but in case someone really needed a dictionary instance now it is easy to convert. For example, in the following code the line d = dict(x) would fail before this change:

from java.util import HashMap
x = HashMap()
x['abc'] = 'def'
x[35] = 'ghi'
d = dict(x)

I also implemented items() for completeness.

ndjensen commented 11 months ago

In reviewing, please check that I decrefed my objects appropriately, as it's been a while since I developed code like that and I don't want to cause crashes (decrefed too much) or leaks (forgot to decref).