Skip to content

SCALA-17 Scala Common Collections #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ lazy val scala_lang = (project in file("scala-lang"))
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test"
)

lazy val scala_core_collections = (project in file("scala-core-collections"))
.settings(
name := "scala-core-collections",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
)

lazy val scala_test = (project in file("scala-test"))
.settings(
name := "scala-test",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.baeldung.scala

import scala.collection.mutable

object ScalaCollections {

// Scala List
val numbersList: List[Int] = List(1, 2, 3, 4)
val emptyList: List[Int] = List()

val numbersListWithOperator: List[Int] = 1 :: 2 :: 3 :: 4 :: Nil
val emptyListWithNil: List[Int] = Nil
val x :: xs = numbersList

// Scala Set
val emptySet: Set[Int] = Set()
val numbersSet: Set[Int] = Set(1, 2, 3, 4)

//Scala Map
val immutableMap: Map[Int, String] = Map(1 -> "a", 2 -> "b")
val mutableMap: mutable.Map[Int, String] = collection.mutable.Map(1 -> "a", 2 -> "b")

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.baeldung.scala

import org.scalatest.{Matchers, _}

class ScalaCollectionsUnitTest extends FlatSpec with Matchers {

"Scala list" should "concatenate using ::: operator" in {
//given
val list1 = List(1, 2)
val list2 = List(3, 4)

//when
val res = list1 ::: list2

//then
res shouldBe List(1, 2, 3, 4)
}

it should "create a list will all identical elements using fill method" in {
//when and then
List.fill(3)(100) shouldBe List(100, 100, 100)
}

it should "reverse should create a reverse all elements of the list" in {
//given
val list = List(1, 2, 3, 4)

//when and then
list.reverse shouldBe List(4, 3, 2, 1)
}

"Scala set" should "return head of the set when .head method invoked" in {
//given
val set = Set(1, 2, 3, 4)

//when and then
set.head shouldBe 1
}

it should "return all element of set except head when .tail method invoked" in {
//given
val set = Set(1, 2, 3, 4)

//when and then
set.tail shouldBe Set(2, 3, 4)
}

"Maps" should "return all keys from the map when .keys method invoked" in {
//given
val map = Map(1 -> "a", 2 -> "b")

//when and then
map.keys shouldBe Set(1, 2)
}

it should "return all values from the map when .values invoked" in {
//given
val map = Map(1 -> "a", 2 -> "b")

//when and then
map.values.toList shouldBe List("a", "b")
}

it should "return None if the key is not found in map when .get method invoked" in {
//given
val map = Map(1 -> "a", 2 -> "b")

//when
val res = map.get(-1)

//then
res.isEmpty shouldBe true
}

it should "return value if the key is found in map using .get method" in {
//given
val map = Map(1 -> "a", 2 -> "b")

//when
val res = map.get(1)

//then
res shouldBe Some("a")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CaseClassesUnitTest {
def givenCaseClass_whenPatternMatched_thenReturnsProperValue() = {
val covidPL = CovidCountryStats("PL", 776, 15366)

val text = covidPL matches {
val text = covidPL match {
case CovidCountryStats("PL", x, y) => "Death rate for Poland is " + x.toFloat / y.toFloat
case _ => "Unknown country"
}
Expand All @@ -29,8 +29,8 @@ class CaseClassesUnitTest {
val covidPL = CovidCountryStats("PL", 776, 15366)
val covidUA = covidPL.copy(countryCode = "UA")

assertEquals("UA", covidUA.countryCodeo)
assertEquals(766, covidUA.deaths)
assertEquals("UA", covidUA.countryCode)
assertEquals(776, covidUA.deaths)
assertEquals(15366, covidUA.confirmedCases)
}

Expand Down