Skip to main content

import - Working with Java hashmaps from Mathematica


I'm using J/Link to access data from an external Java program. The data are represented as a ConcurrentHashMap (see the official specification here).


link = JavaNew["org.ddpclient.DDPTestClient"];


When I evaluate data = link@mCollections in my Mathematica notebook to access mCollections field where the data of interest are stored, I get a HashMap object:


« JavaObject[java.util.HashMap]»


Is there a way to convert this HashMap into a native Mathematica list (or nested list, to be more precise) so that I could work with it?


I know that one can call a toString[] method on the HashMap object to convert it to a string. In my case, data@toString[] yields something like this:



{G9fuqeYuiQpcmL8MW={playerId=zxcgf24ta, lastSeen=null, status=0.0, \ active=true}, aiej2mQppBAefhad7={playerId=lklu453da1, lastSeen=null, \ status=0.0, active=true}, Bbd8YqAx8yFcdcMcz={playerId=poo10alll235, \ lastSeen=null, status=0.0, active=true}, \ Gnt3KKrrgypgFEorp={playerId=hjffxz535cd, lastSeen=null, status=0.0, \ active=true}, ioydvWm7GacPg5DyM={playerId=asdfa123sa, lastSeen=null, \ status=1.0, active=false}, 8vZ6i73t6mbwTB63D={playerId=66df24hl, \ lastSeen=null, status=0.0, active=true}}


It is probably possible to manually parse this, but I am afraid it would be really difficult to handle all the possible edge cases that could arise during the conversion. Is there a better way?




Comments