val donuts1: Set[String] = Set("Plain Donut", "Strawberry Donut", "Glazed Donut")val donuts2: Set[String] = Set("Plain Donut", "Chocolate Donut", "Vanilla Donut")// find the common elements between two Sets using intersect functionval inter = donuts1 intersect donuts2// find the common elements between two Sets using & functionval common = donuts1 & donuts2
两个集合的交集,那就只有一个,所以输出结果如下。
scala>val inter = donuts1 intersect donuts2inter: scala.collection.immutable.Set[String] = Set(Plain Donut)scala>val common = donuts1 & donuts2common: scala.collection.immutable.Set[String] = Set(Plain Donut)