I am porting some software and it's working in Swift and JavaScript, but I cannot figure out how to do it in Java.
The JavaScript version looks like:
var obj = { "A": {
"1": (params) => { console.log("A1", params); },
"2": (params) => { console.log("A2", params); }
}, // A
"B": {
"1": (params) => { console.log("B1", params); },
"2": (params) => { console.log("B2", params);}
}, //B
}; // obj
Where at runtime, the application engine calls
var a = 'A';
var i = '2';
obj[a][i](params);
I for the life of me cannot figure out how to do this in Java. One key constraint (no pun intended) is that the code must be structurally similar to the other ports.
Previously, I was trying to use Object[][], with
Map<String, Object> map = Stream.of(obj).collect(Collectors.toMap(data -> (String) data[0], data -> (Object) data[1]));
to be able to embed some Map<String, Integer> objects. Example:
Map<String, Integer> colorMap = Stream.of(new Object[][] {
{"black", 0x000000},
{"navy", 0x000080},
};
I tried to arrive at similar code, but Java did not like it, because lambdas and Object are not compatible.
The code I am looking to arrive at is:
Map<String, Map<String, Callable<Void> > > obj = Stream.of(new Object[][] {
{"A", Stream.of(new Object[][] {
{"1", (params)->{ System.out.println("A1"); } },
{"2", (params)->{ System.out.println("A2"); } } }),
{"B", Stream.of(new Object[][] {
{"1", (params)->{ System.out.println("B1"); } },
{"2", (params)->{ System.out.println("B2"); } } } )}
}
});
...
// call the function (assuming A and 1 exist)
obj.get("A").get("1")(params);
But I am stumped on how to convert this to being able to use lambdas. The error I keep getting is:
error: incompatible types: Object is not a functional interface
or error: lambda expression not expected here