From: Jon <jo...@li...> - 2010-03-26 18:52:31
|
Thanks, Santosh. Iterating a Java Map in Java isn't what I'm trying to figure out, though; it's taking a Java Map object, passing it to Jython, and iterating using standard Python idioms. If I have a Java Map, I can pass it from Java to a Jython function using ScriptingEngine.invokeFunction(). The following works: def test(mapObj): for key in mapObj: # mapObj implements java.util.Map print(mapObj[key]) (and this alone undeniably rocks!) But the following don't work: def test(mapObj): for key, value in mapObj.items(): # items() is not present on mapObj print("%s = %s" % (key, value)) def test(mapObj): for key, value in mapObj.iteritems(): # iteritems() is not present on mapObj print("%s = %s" % (key, value)) I'm trying to figure out the best way for supporting as much of the standard Python dictionary methods on a java.util.Map as I can. Thanks! Jon On Fri, Mar 26, 2010 at 2:39 PM, Santosh Tiwari <ti...@gm...> wrote: > You can iterate a java map in many ways. > > > java.util.Map<Object, Object> hashMap = new ...; > java.util.Iterator<Object> iter = hashMap.keySet().iterator(); > while(iter.hasNext()) { > Object key = iter.next(); > Object value = hashMap.get(key); > //do something with key and object > } > > Template the above code by your type. > > > > > On Fri, Mar 26, 2010 at 8:41 AM, Jon <jo...@li...> wrote: > >> Hi all, >> >> I feel like this question is probably a FAQ, but I haven't been able to >> turn up any documentation--please point me in the right direction if there's >> anything relevant. >> >> I've got a class in Java that extends java.util.HashMap: >> >> class Foo extends HashMap { /* stuff */ } >> >> I'm using Jython in an embedded manner to pass instances of Foo to Jython >> scripts for processing. On the Jython side, I have no trouble indexing into >> the object, i.e. baz = myFoo['bar'], but a lot of the standard dictionary >> methods aren't available. I'd like to be able to do this: >> >> for k, v in myFoo.items(): # myFoo is from the Java side >> if (k == 'bar') >> print(v) >> >> Specifically, I'd like to be able to use non-modifying dictionary methods, >> like items(), iteritems(), iterkeys(), itervalues(). How do I get there from >> here? I'm fine with an 80/20 solution; PyDictionary looks like what I need, >> except that I do not want the objects stored in the dict to inherit from >> PyObject (as a rule, the objects I'm storing do have nice Jython >> conversions). So, I want the class to look like a PyDictionary to the >> greatest extent possible, but to have its own implementation (which is >> complex and can't be swapped out). >> >> As an aside, what's the difference between PyDictionary and >> PyDictionaryDerived? >> >> Thanks in advance. I've been amazed with how easy it's been to get Jython >> embedding up and running; it's a huge win for my application. >> >> >> Jon >> >> >> >> ------------------------------------------------------------------------------ >> Download Intel® Parallel Studio Eval >> Try the new software tools for yourself. Speed compiling, find bugs >> proactively, and fine-tune applications for parallel performance. >> See why Intel Parallel Studio got high marks during beta. >> http://p.sf.net/sfu/intel-sw-dev >> _______________________________________________ >> Jython-users mailing list >> Jyt...@li... >> https://lists.sourceforge.net/lists/listinfo/jython-users >> >> > > > -- > Santosh Tiwari > ti...@gm... > |