Java Reference
In-Depth Information
Converting an Array to a String
You can convert an array to a string in any way you want. However, the
toString()
and
toLocaleString()
methods provide two built-in implementations to convert the
array elements to a string. The
toString()
method returns a string that is returned
by calling the
join()
method on the array without specifying any separator. That is, it
returns the list of elements as a string where elements are separated by commas. The
toLocaleString()
method calls the
toLocaleString()
method of all array elements,
concatenates the returned strings using a local-specific separator and return the final
string. You have been using the
toString()
method of the
Array
object whenever you
used the array object as an argument to the
print()
method or used it with the string
concatenation operator.
Stream-Like Processing of Arrays
Java 8 introduced the Streams API that lets you process Java collections as stream using
several patterns such as map-filter-reduce. The
Array
object in Nashorn provides
methods to apply stream-like processing to arrays. Such methods are:
•
map()
•
filter()
•
reduce()
•
reduceRight()
•
forEach()
•
some()
every()
I have already discussed the last three methods in detail in previous section. I will
discuss other methods in this section.
The
map()
method maps elements of an array to another values and returns a new
array containing the mapped values. The original array is not modified. Its signature is:
•
map(callback, thisArg)
Search WWH ::
Custom Search