Java Reference
In-Depth Information
it is possible to return a nashorn array to Java code from a nashorn function. You
need to extract the elements of the native array in Java code, and therefore, you need to
use nashorn-specific classes in Java. this approach is not advised. You should convert the
nashorn array to a Java array and return the Java array from a nashorn function instead, so
the Java code deals only with Java classes.
Tip
Arrays to Java Collections Conversions
Whenever possible, Nashorn provides automatic conversion from Nashorn arrays to
Java arrays, List s, and Map s. Converting Nashorn arrays to Java arrays and List s is
straightforward. When a Nashorn array is converted to a Java Map , the indexes of array
elements become keys in the Map and element's values become the values for the
corresponding indexes. Consider a Java class named ArrayConversion as shown in
Listing 7-7. It contains four methods that accept arrays, List s, and Map s as arguments.
Listing 7-7. A Java Class Named ArrayConversion
// ArrayConversion.java
package com.jdojo.script;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class ArrayConversion {
public static void printInts(int[] ids) {
System.out.print("Inside printInts():");
System.out.println(Arrays.toString(ids));
}
public static void printDoubles(double[] salaries) {
System.out.print("Inside printDoubles(double[]):");
System.out.println(Arrays.toString(salaries));
}
public static void printList(List<Integer> idsList) {
System.out.print("Inside printList():");
System.out.println(idsList);
}
 
 
Search WWH ::




Custom Search