Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through Java collection in Javascript

I'm trying to do scripting with Javascript in a Java program. I haven't found a way to iterate through a Java collection in Javascript. If I call the iterator() method for the collection, I get the method names instead of the elements.

Here's a sample code:

function getValue(row, components) {
    var apartment = components.get(0);
    var rooms = apartment.getRooms();
    for (var room in rooms.iterator()) {
        println(room);
    }
    return rooms.toString();
}

The apartment.getRooms() returns a collection of rooms. When I study the value returned by this function, I know that its content are correct, but the values that get printed are the method names.

I invoke the Javascript from my Java program like this:

getInvocable().invokeFunction("getValue", row, components);
like image 771
user1983702 Avatar asked Jan 16 '13 13:01

user1983702


1 Answers

It seems that if I do the iteration as follows:

function getValue(row, components) {
    var apartment = components.get(0);
    var rooms = apartment.getRelated();
    for (var iterator = rooms.iterator(); iterator.hasNext();) {
        var room = iterator.next();
        println(room);
    }
    return rooms.toString();
}

It works.

like image 184
user1983702 Avatar answered Nov 14 '22 21:11

user1983702