- Scala Collections - Discussion
- Scala Collections - Useful Resources
- Scala Collections - Quick Guide
- Scala Collections - zip
- Scala Collections - scan
- Scala Collections - reduce
- Scala Collections - partition
- Scala Collections - map
- Scala Collections - foldRight
- Scala Collections - foldLeft
- Scala Collections - fold
- Scala Collections - flatten
- Scala Collections - flatMap
- Scala Collections - find
- Scala Collections - filter
- Scala Collections - dropWhile
- Scala Collections - drop
- Scala Collections - Stream
- Scala Collections - Stack
- Scala Collections - Seq
- Scala Collections - Tuple
- Scala Collections - Queue
- Scala Collections - Option
- Scala Collections - Iterator
- Scala Collections - ListMap
- Scala Collections - HashMap
- Scala Collections - Map
- Scala Collections - TreeSet
- Scala Collections - HashSet
- Scala Collections - BitSet
- Scala Collections - Set
- Scala Collections - Vector
- Scala Collections - ListSet
- Scala Collections - ListBuffer
- Scala Collections - List
- Scala Collections - ArrayBuffer
- Scala Collections - Array using Range
- Scala Collections - Multi-Dimensional Array
- Scala Collections - Array
- Scala Collections - Environment Setup
- Scala Collections - Overview
- Scala Collections - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Scala Collections - Set
Scala Set is a collection of pairwise different elements of the same type. In other words, a Set is a collection that contains no duppcate elements. There are two kinds of Sets, the immutable and the mutable. The difference between mutable and immutable objects is that when an object is immutable, the object itself can t be changed.
By default, Scala uses the immutable Set. If you want to use the mutable Set, you ll have to import scala.collection.mutable.Set class exppcitly. If you want to use both mutable and immutable sets in the same collection, then you can continue to refer to the immutable Set as Set but you can refer to the mutable Set as mutable.Set.
Here is how you can declare immutable Sets −
Syntax
// Empty set of integer type var s : Set[Int] = Set() // Set of integer type var s : Set[Int] = Set(1,3,5,7) or var s = Set(1,3,5,7)
While defining an empty set, the type annotation is necessary as the system needs to assign a concrete type to variable.
Basic Operations on set
All operations on sets can be expressed in terms of the following three methods −
Sr.No | Methods & Description |
---|---|
1 |
head This method returns the first element of a set. |
2 |
tail This method returns a set consisting of all elements except the first. |
3 |
isEmpty This method returns true if the set is empty otherwise false. |
Try the following example showing usage of the basic operational methods −
Example
object Demo { def main(args: Array[String]) { val fruit = Set("apples", "oranges", "pears") val nums: Set[Int] = Set() println( "Head of fruit : " + fruit.head ) println( "Tail of fruit : " + fruit.tail ) println( "Check if fruit is empty : " + fruit.isEmpty ) println( "Check if nums is empty : " + nums.isEmpty ) } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
>scalac Demo.scala >scala Demo
Output
Head of fruit : apples Tail of fruit : Set(oranges, pears) Check if fruit is empty : false Check if nums is empty : true
Concatenating Sets
You can use either ++ operator or Set.++() method to concatenate two or more sets, but while adding sets it will remove duppcate elements.
The Following is the example to concatenate two sets.
Example
object Demo { def main(args: Array[String]) { val fruit1 = Set("apples", "oranges", "pears") val fruit2 = Set("mangoes", "banana") // use two or more sets with ++ as operator var fruit = fruit1 ++ fruit2 println( "fruit1 ++ fruit2 : " + fruit ) // use two sets with ++ as method fruit = fruit1.++(fruit2) println( "fruit1.++(fruit2) : " + fruit ) } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
>scalac Demo.scala >scala Demo
Output
fruit1 ++ fruit2 : Set(banana, apples, mangoes, pears, oranges) fruit1.++(fruit2) : Set(banana, apples, mangoes, pears, oranges)
Find Max, Min Elements in a Set
You can use Set.min method to find out the minimum and Set.max method to find out the maximum of the elements available in a set. Following is the example to show the program.
Example
object Demo { def main(args: Array[String]) { val num = Set(5,6,9,20,30,45) // find min and max of the elements println( "Min element in Set(5,6,9,20,30,45) : " + num.min ) println( "Max element in Set(5,6,9,20,30,45) : " + num.max ) } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
>scalac Demo.scala >scala Demo
Output
Min element in Set(5,6,9,20,30,45) : 5 Max element in Set(5,6,9,20,30,45) : 45
Find Common Values Insets
You can use either Set.& method or Set.intersect method to find out the common values between two sets. Try the following example to show the usage.
Example
object Demo { def main(args: Array[String]) { val num1 = Set(5,6,9,20,30,45) val num2 = Set(50,60,9,20,35,55) // find common elements between two sets println( "num1.&(num2) : " + num1.&(num2) ) println( "num1.intersect(num2) : " + num1.intersect(num2) ) } }
Save the above program in Demo.scala. The following commands are used to compile and execute this program.
Command
>scalac Demo.scala >scala Demo
Output
num1.&(num2) : Set(20, 9) num1.intersect(num2) : Set(20, 9)Advertisements