Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, TreeSet
was a type of sorted set based on a binary search tree. It provided an efficient implementation for insertion, removal, and lookup of elements in a sorted order. However, starting from Scala 2.13, the collections library was reworked, and TreeSet
was replaced with SortedSet
which uses a tree-based implementation behind the scenes.
To use a TreeSet
-like structure in Scala 2.13 and later, you'd use SortedSet
. Here's how you can use it:
import scala.collection.immutable.SortedSet val numbers = SortedSet(4, 2, 8, 1, 5) println(numbers) // Output: SortedSet(1, 2, 4, 5, 8)
Notice how the numbers are automatically sorted.
val addedNumbers = numbers + 6 + 7 println(addedNumbers) // Output: SortedSet(1, 2, 4, 5, 6, 7, 8) val removedNumbers = addedNumbers - 5 - 7 println(removedNumbers) // Output: SortedSet(1, 2, 4, 6, 8)
While SortedSet
provides sorted iteration, it doesn't offer efficient indexed access (like accessing the 3rd element directly). However, you can still iterate over it:
for (n <- numbers) { println(n) }
You can perform various operations on SortedSet
like union, intersection, and difference:
val set1 = SortedSet(1, 2, 3) val set2 = SortedSet(3, 4, 5) println(set1 union set2) // Output: SortedSet(1, 2, 3, 4, 5) println(set1 intersect set2) // Output: SortedSet(3) println(set1 diff set2) // Output: SortedSet(1, 2)
If you need a custom ordering for your SortedSet
, you can provide an implicit Ordering
:
implicit val reverseOrder: Ordering[Int] = Ordering[Int].reverse val reversedNumbers = SortedSet(4, 2, 8, 1, 5) println(reversedNumbers) // Output: SortedSet(8, 5, 4, 2, 1)
Here, the SortedSet
is ordered in descending instead of the default ascending order.
In conclusion, while TreeSet
as a name isn't present in Scala 2.13+, its functionality and more are available through SortedSet
. It's an excellent tool when you need a collection of unique elements kept in a specific order.
How to use TreeSet in Scala:
TreeSet
is a collection in Scala that implements a sorted set using a Red-Black Tree. It automatically maintains elements in sorted order.// Using TreeSet import scala.collection.immutable.TreeSet val treeSet = TreeSet(3, 1, 4, 1, 5, 9, 2, 6, 5, 3) println(treeSet) // Output: TreeSet(1, 2, 3, 4, 5, 6, 9)
Immutable TreeSet in Scala:
TreeSet
in Scala is immutable by default. Any operation that modifies the set creates a new set with the updated elements.// Immutable TreeSet val immutableTreeSet = TreeSet(1, 3, 2, 4) val updatedTreeSet = immutableTreeSet + 5 println(updatedTreeSet) // Output: TreeSet(1, 2, 3, 4, 5)
Sorting elements with TreeSet in Scala:
TreeSet
automatically sorts elements. It's particularly useful when you need a collection where elements are always ordered.// Sorting with TreeSet val unsortedSet = TreeSet(3, 1, 4, 1, 5, 9, 2, 6, 5, 3) println(unsortedSet) // Output: TreeSet(1, 2, 3, 4, 5, 6, 9)
Creating a TreeSet with custom ordering in Scala:
TreeSet
by providing an implicit Ordering
.// Custom ordering in TreeSet import scala.collection.immutable.TreeSet case class Person(name: String, age: Int) implicit val ordering: Ordering[Person] = Ordering.by(_.age) val customOrderingSet = TreeSet(Person("Alice", 25), Person("Bob", 20), Person("Charlie", 30)) println(customOrderingSet) // Output: TreeSet(Person(Bob,20), Person(Alice,25), Person(Charlie,30))
Adding and removing elements in TreeSet Scala:
+
and removed with -
operators. Since TreeSet
is immutable, these operations return new sets.// Adding and removing elements in TreeSet val initialSet = TreeSet(1, 2, 3) val addedSet = initialSet + 4 val removedSet = addedSet - 2 println(addedSet) // Output: TreeSet(1, 2, 3, 4) println(removedSet) // Output: TreeSet(1, 3, 4)
TreeSet API in Scala:
TreeSet
provides various methods for set operations, such as +
, -
, head
, tail
, union
, intersect
, diff
, and more.// TreeSet API val set1 = TreeSet(1, 2, 3) val set2 = TreeSet(2, 3, 4) val unionSet = set1 union set2 val intersectionSet = set1 intersect set2 val differenceSet = set1 diff set2 println(unionSet) // Output: TreeSet(1, 2, 3, 4) println(intersectionSet) // Output: TreeSet(2, 3) println(differenceSet) // Output: TreeSet(1)