Conversion from a Java Set to a Scala Set Last Updated : 21 Nov, 2019 Comments Improve Suggest changes Like Article Like Report 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. Example:1# Scala // Scala program of converting a Java Set // to a Scala Set // Importing JavaConversions.asScalaSet import scala.collection.JavaConversions.asScalaSet // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a Java List val jlist = java.util.Arrays.asList(14, 15, 16) // Creating a java Set val jSet = new java.util.HashSet[Int]() // Adding all the elements of the // list to the set val x = jSet.addAll(jlist) // Converting from java Set // to Scala Set val results = asScalaSet(jSet) // Displays results println(results) } } Output: 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. Example:2# Scala // Scala program of converting a Java list // to a Scala Buffer // Importing JavaConversions.asScalaSet import scala.collection.JavaConversions.asScalaSet // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a Java List val jlist = java.util.Arrays.asList(11, 9, 5) // Creating a java Set val jSet = new java.util.HashSet[Int]() // Adding all the elements of the // list to the set val x = jSet.addAll(jlist) // Converting from java Set // to Scala Set val results = asScalaSet(jSet) // Displays results println(results) } } Output: 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. Comment More infoAdvertise with us Next Article Conversion from a Java Set to a Scala Set nidhi1352singh Follow Improve Article Tags : Scala Java-Collections Scala scala-collection Practice Tags : Java-Collections Similar Reads Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read HashMap in Java In Java, HashMap is part of the Java Collections Framework and is found in the java.util package. It provides the basic implementation of the Map interface in Java. HashMap stores data in (key, value) pairs. Each key is associated with a value, and you can access the value by using the corresponding 15+ min read ArrayList in Java Java ArrayList is a part of the collections framework and it is a class of java.util package. It provides us with dynamic-sized arrays in Java. The main advantage of ArrayList is that, unlike normal arrays, we don't need to mention the size when creating ArrayList. It automatically adjusts its capac 9 min read Java List Interface The List Interface in Java extends the Collection Interface and is a part of the java.util package. It is used to store the ordered collections of elements. In a Java List, we can organize and manage the data sequentially. Key Features:Maintained the order of elements in which they are added.Allows 15+ min read Map Interface in Java In Java, the Map Interface is part of the java.util package and represents a mapping between a key and a value. The Java Map interface is not a subtype of the Collections interface. So, it behaves differently from the rest of the collection types.Key Features:No Duplicates in Keys: Keys should be un 11 min read Stack Class in Java The Java Collection framework provides a Stack class, which implements a Stack data structure. The class is based on the basic principle of LIFO (last-in-first-out). Besides the basic push and pop operations, the class also provides three more functions, such as empty, search, and peek. The Stack cl 11 min read LinkedList in Java Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure, which is a linear data structure where the elements are not stored in contiguous locations, and every element is a separate object with a data part and an 12 min read Set in Java The Set Interface is present in java.util package and extends the Collection interface. It is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the mathematical set. This interface adds a feature that restricts the insertion of duplicat 14 min read Queue Interface In Java The Queue Interface is a part of java.util package and extends the Collection interface. It stores and processes the data in order means elements are inserted at the end and removed from the front. Key Features:Most implementations, like PriorityQueue, do not allow null elements.Implementation Class 11 min read Java Collections Interview Questions and Answers Java Collection Framework was introduced in JDK 1.2 which contains all the collection classes and interfaces. Java Collection is a framework that provides a mechanism to store and manipulate the collection of objects. It allows developers to access prepackaged data structures and algorithms for mani 15+ min read Like