Accessing Java Arrays and Collections in Nashorn - Java Scripting

Java examples for Scripting:Bindings

Introduction

Use the Java.type function to coerce Java arrays to JavaScript.

var intArray = Java.type("int[]"); 
var intArr = new intArray(5); 
intArr[0] = 0; 
intArr[1] = 1; 
intArr[0] 
intArr.length 
 

To access a Java Collection type, you call the Java.type function, passing the name of the type.

var ArrayList = Java.type("java.util.ArrayList") 
var array = new ArrayList(); 
array.add('hi'); 
array.add('bye'); 
array 

var map = Java.type("java.util.HashMap") 
var jsMap = new map(); 
jsMap.put(0, "first"); 
jsMap.put(1, "second"); 
jsMap.get(1);

Related Tutorials