Friday, October 16, 2015

Common operations for mutable Set

// scala code
object Test {
  import collection.mutable.{Set => MSet}
  val xs = MSet[Int]((1 to 10): _*)
  xs += 11
  xs == MSet[Int]((1 to 11): _*)
  xs += (12, 13, 14)
  xs == MSet((1 to 14): _*)
  xs add 15
  xs == MSet((1 to 15): _*)
  xs ++= (16 to 18)
  xs == MSet((1 to 18): _*)
  xs -= 18
  xs == MSet((1 to 17): _*)
  xs -= (16, 17)
  xs == MSet((1 to 15): _*)
  xs --= (13 to 15)
  xs == MSet((1 to 12): _*)
  xs remove 12
  xs == MSet((1 to 11): _*)
  xs.retain(_ > 5)
  xs == MSet((6 to 11): _*)
  val xs1 = xs.clone()
  xs(6) = false // remove 6
  xs(12) = true // add 12
  xs == MSet((7 to 12): _*)
  xs.clear
  xs.isEmpty // true
  xs == MSet() // true
}

0 comments: