//Set的特点:唯一(元素不相同);无序
case class Book(var bookName:String,var author:String,var price:Double){
}
object test27 {
def main(args: Array[String]): Unit = {
//定义一个可变set
val set1 = scala.collection.mutable.Set[Book]()
//向Set中添加两条一样的数据
val book1 = new Book("西游记","吴承恩",99)
val book2 = new Book("西游记","吴承恩",99)
val book3 = new Book("西游记","吴承恩",99)
//book1 == book2?
println(book1 == book2) // false 因为 book1和book2 地址不同
set1 += book1
set1 += book2
set1 += book3
set1.foreach(s=>{
println(s.author)
})
}
}
Scala的set
懒惰的橘猫2024-11-14 18:13