Skip to content

Instantly share code, notes, and snippets.

@Amelia-Lopez
Last active January 25, 2016 19:09
Show Gist options
  • Save Amelia-Lopez/3fa1d5c7bb97271828dd to your computer and use it in GitHub Desktop.
Save Amelia-Lopez/3fa1d5c7bb97271828dd to your computer and use it in GitHub Desktop.
Scala diff vs intersect
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_79).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import collection.immutable._
import collection.immutable._
scala> import math.Ordering._
import math.Ordering._
scala> var headers = new TreeMap[String, Any]()(Ordering.by(_.toLowerCase))
headers: scala.collection.immutable.TreeMap[String,Any] = Map()
scala> headers = headers + ("apple" -> 1) + ("potato" -> 2)
headers: scala.collection.immutable.TreeMap[String,Any] = Map(apple -> 1, potato -> 2)
scala> headers.contains("apple")
res0: Boolean = true
scala> headers.contains("APPLE")
res1: Boolean = true
scala> headers.keySet.contains("apple")
res2: Boolean = true
scala> headers.keySet.contains("APPLE")
res3: Boolean = true
scala> headers.keySet.diff(Set("banana"))
res4: scala.collection.immutable.SortedSet[String] = Set(apple, potato)
scala> headers.keySet.diff(Set("apple"))
res5: scala.collection.immutable.SortedSet[String] = TreeSet(potato)
scala> headers.keySet.diff(Set("APPLE"))
res6: scala.collection.immutable.SortedSet[String] = TreeSet(potato)
scala> headers.keySet.intersect(Set("banana"))
res7: scala.collection.immutable.SortedSet[String] = TreeSet()
scala> headers.keySet.intersect(Set("apple"))
res8: scala.collection.immutable.SortedSet[String] = TreeSet(apple)
scala> headers.keySet.intersect(Set("APPLE"))
res9: scala.collection.immutable.SortedSet[String] = TreeSet()
scala> Set("banana").intersect(headers.keySet)
res16: scala.collection.immutable.Set[String] = Set()
scala> Set("apple").intersect(headers.keySet)
res17: scala.collection.immutable.Set[String] = Set(apple)
scala> Set("APPLE").intersect(headers.keySet)
res19: scala.collection.immutable.Set[String] = Set(APPLE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment