Program to convert Java Set of floats to a Traversable in Scala
A java Set of floats can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work.
Now, lets see some examples and then discuss how it works in details.
Example:1#
// Scala program to convert Java set // to a Traversable in Scala // Importing Scala's JavaConversions object import scala.collection.JavaConversions. _ // Creating object object GfG { // Main method def main(args : Array[String]) { // Creating set of floats in Java val set = new java.util.HashSet[Float]() // Adding floats to the set set.add( 4.1 f) set.add( 7.1 f) set.add( 8.1 f) // Converting set to a Traversable val tra = set.toTraversable // Displays traversable println(tra) } } |
Output:
Set(4.1, 7.1, 8.1)
Example:2#
// Scala program to convert Java set // to a Traversable in Scala // Importing Scala's JavaConversions object import scala.collection.JavaConversions. _ // Creating object object GfG { // Main method def main(args : Array[String]) { // Creating set of floats in Java val set = new java.util.HashSet[Float]() // Adding floats to the set set.add( 5.2 f) set.add( 4.5 f) set.add( 9.2 f) // Converting set to a Traversable val tra = set.toTraversable // Displays traversable println(tra) } } |
Output:
Set(5.2, 4.5, 9.2)
Please Login to comment...