A Java Set can be converted to a Scala Set by importing JavaConversions.asScalaSet method. Here, we need to call asScalaSet method which has a java Set as its argument. Therefore, this method returns a Scala Set.
Now, lets see some examples.
Set(14, 15, 16)
So, a Scala Set is returned here. In the above example firstly, we have created a Java list then we have declared a Java Set Where, we have added all the elements of the Java list to the Java Set utilizing addAll method. After that the stated Java Set is converted to a Scala Set utilizing asScalaSet method.
Lets see one more example.
Set(5, 9, 11)
Therefore, here also a Set is returned. Moreover, the list stated here is given in a proper order but a Set needs to be in a proper order so, the Set which is returned as output is in proper order.
- Conversion from Java List to Scala Buffer
- Array to ArrayList Conversion in Java
- Conversion of Array To ArrayList in Java
- Conversion of Java Maps to List
- ArrayList to Array Conversion in Java : toArray() Methods
- Scala vs Java
- Scala and Java Interoperability
- Program to print Java Set in Scala
- Program to convert Java Set to a Vector in Scala
- Program to convert Java Set to an Iterator in Scala
- Program to print Java Set of Strings in Scala
- Program to print Java Set of characters in Scala
- Program to convert Java Set to a String in Scala
- Program to convert Java list to Set in Scala
- Program to convert Java Set to List in Scala
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.