user1790894
user1790894

Reputation: 417

R equivalent of Java map

I would like to pass a key/value pair from my R code to a java function. My java function has argument Map<String,String > .

How can I write R function which calls my Java function and pass values to map ??

EDIT :

config <- list(Portname="PORT.H.2",MktValue="8000000",WtScheme="Closed")
createPortfolio<-function(config)
{
   m <- .jnew("java/util/HashMap")
   for( key in names(config)){
     m$put( key, config[key])

}
m

getting

  Error in FUN(X[[2L]], ...) : 
  Sorry, parameter type `NA' is ambiguous or not supported.

Upvotes: 2

Views: 1468

Answers (3)

xiroV
xiroV

Reputation: 41

For people, like myself, who had this issue and don't have access to the Java code, it seems to be possible to make a HashMap, and cast it to a Map, like this:

m <- .jnew("java/util/HashMap")
m$put( "key", "value" )
map <- .jcast(m, "java/util/Map")

Upvotes: 0

Simon Urbanek
Simon Urbanek

Reputation: 13932

I think you meant

for (key in names(config)) m$put(key, config[[key]])

since you want to pass string as value to put and not a list.

(Consider asking on the rJava mailing list stats-rosuda-devel to get a more prompt answer)

Upvotes: 1

rlegendi
rlegendi

Reputation: 10606

How about trying something like this?

m <- .jnew("java/util/HashMap")
m$put( "key", "value" )

Upvotes: 1

Related Questions