Scala Collections - ListSet



Scala Set is a collection of pairwise different elements of the same type. In other words, a Set is a collection that contains no duplicate elements. ListSet implements immutable sets and uses list structure. Elements insertion order is preserved while storing the elements.

Declaring ListSet Variables

The following is the syntax for declaring an ListSet variable.

Syntax

var z : ListSet[String] = ListSet("Zara","Nuha","Ayan")

Here, z is declared as an list-set of Strings which has three members. Values can be added by using commands like the following −

Command

var myList1: ListSet[String] = myList + "Naira";

Processing ListSet

Below is an example program of showing how to create, initialize and process ListSet −

Example

import scala.collection.immutable.ListSet
object Demo {
   def main(args: Array[String]) = {
      var myList: ListSet[String] = ListSet("Zara","Nuha","Ayan");
      // Add an element
      var myList1: ListSet[String] = myList + "Naira";
      // Remove an element
      var myList2: ListSet[String] = myList - "Nuha";
      // Create empty set
      var myList3: ListSet[String] = ListSet.empty[String];
      println(myList);
      println(myList1);
      println(myList2);
      println(myList3);	  
   }
}

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

ListSet(Ayan, Nuha, Zara)
ListSet(Naira, Ayan, Nuha, Zara)
ListSet(Ayan, Zara)
ListSet()

Checking Membership

You can check if a value is present in a ListSet using the contains method. This method returns true if the value is present and false otherwise.

Example

Try following example for checking membership whether these are existed or not -

import scala.collection.immutable.ListSet

object Demo {
   def main(args: Array[String]) = {
      var mySet = ListSet("Zara", "Nuha", "Ayan")
      // Check if the ListSet contains "Nuha"
      println(mySet.contains("Nuha"))
      // Check if the ListSet contains "Naira"
      println(mySet.contains("Naira"))
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

true
false

Common Set Operations

You can perform various common set operations such as union, intersection, and difference on ListSet. These operations are useful for comparing and combining sets.

  • Union: For all unique elements from both sets.
  • Intersection: For common unique elements from both sets.
  • Difference: For elements from the first set that are not in the second set.

Example

import scala.collection.immutable.ListSet

object Demo {
   def main(args: Array[String]) = {
      val set1 = ListSet("Zara", "Nuha", "Ayan")
      val set2 = ListSet("Ayan", "Naira", "Maira")

      // Perform union operation
      val unionSet = set1 union set2
      println("Union: " + unionSet)

      // Perform intersection operation
      val intersectionSet = set1 intersect set2
      println("Intersection: " + intersectionSet)

      // Perform difference operation
      val differenceSet = set1 diff set2
      println("Difference: " + differenceSet)
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

Union: ListSet(Naira, Maira, Ayan, Nuha, Zara)
Intersection: ListSet(Ayan)
Difference: ListSet(Nuha, Zara)

Finding Maximum and Minimum Elements

You can find the maximum and minimum elements in a ListSet using the max and min methods, respectively.

Example

Try following example for finding min and max elements in ListSet -

import scala.collection.immutable.ListSet

object Demo {
   def main(args: Array[String]) = {
      val numbers = ListSet(5, 2, 8, 1, 4)
      // Find the maximum element
      println("Max element: " + numbers.max)
      // Find the minimum element
      println("Min element: " + numbers.min)
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

Max element: 8
Min element: 1

Subset and Superset

You can check if one ListSet is a subset of another using the subsetOf method. Similarly, you can check if a set is a superset of another using the supersetOf method.

Example

Try following example for checking subset and superset in ListSet -

import scala.collection.immutable.ListSet

object Demo {
   def main(args: Array[String]) = {
      val set1 = ListSet("Zara", "Nuha")
      val set2 = ListSet("Zara", "Nuha", "Ayan")
      // Check if set1 is a subset of set2
      val isSubset = set1.subsetOf(set2)
      println("Is set1 a subset of set2? " + isSubset)
      // Check if set2 is a superset of set1
      val isSuperset = set2.subsetOf(set1)
      println("Is set2 a superset of set1? " + isSuperset)
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

Is set1 a subset of set2? true
Is set2 a superset of set1? false

Transforming Elements

You can transform the elements of a ListSet using the map method. This method applies a function to each element of the set and returns a new set with the transformed elements.

Example

Try following example for doubling elements of ListSet -

import scala.collection.immutable.ListSet

object Demo {
   def main(args: Array[String]) = {
      val numbers = ListSet(1, 2, 3, 4, 5)
      // Transform the elements by doubling them
      val doubled = numbers.map(_ * 2)
      println(doubled)
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

ListSet(2, 4, 6, 8, 10)

Filtering Elements

You can filter elements of a ListSet using the filter method. This method returns a new set containing only the elements that satisfy a given predicate.

Example

Try following example for filtering only even numbers in ListSet -

import scala.collection.immutable.ListSet

object Demo {
   def main(args: Array[String]) = {
      val numbers = ListSet(1, 2, 3, 4, 5)
      // Filter elements that are even
      val evens = numbers.filter(_ % 2 == 0)
      println(evens)
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

ListSet(2, 4)

Scala ListSet Summary

  • ListSet in Scala is a collection of unique elements that keeps the insertion order.
  • ListSet is immutable by default. There is no mutable
  • You can add, remove, and check for elements in a ListSet.
  • Common set operations such as union, intersection, and difference can be performed on ListSet.
  • You can find the maximum and minimum elements in a set.
  • You can check for subset and superset relationships between sets.
  • You can transform and filter elements in a ListSet.
Advertisements