Program to convert Java list of floats to an Iterable in Scala
A java list of floats can be converted to an Iterable in Scala by utilizing toIterable method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur.
Now, lets see some examples and then discuss how it works in details.
Example:1#
// Scala program to convert Java list // to an Iterable in Scala // Importing Scala's JavaConversions object import scala.collection.JavaConversions. _ // Creating object object GfG { // Main method def main(args : Array[String]) { // Creating list of floats in Java val list = new java.util.ArrayList[Float]() // Adding floats to the list list.add( 8.1 f) list.add( 9.1 f) list.add( 10.1 f) // Converting list to an Iterable val iterab = list.toIterable // Displays output println(iterab) } } |
Output:
Buffer(8.1, 9.1, 10.1)
Example:2#
// Scala program to convert Java list // to an Iterable in Scala // Importing Scala's JavaConversions object import scala.collection.JavaConversions. _ // Creating object object GfG { // Main method def main(args : Array[String]) { // Creating list of floats in Java val list = new java.util.ArrayList[Float]() // Adding floats to the list list.add( 3.4 f) list.add( 2.6 f) list.add( 1.1 f) // Converting list to an Iterable val iterab = list.toIterable // Displays output println(iterab) } } |
Output:
Buffer(3.4, 2.6, 1.1)
Please Login to comment...