我有两组喷气机,我想接上两组的交叉点。 这套装置中的物体就是这样。
@BeanInfo
class User {
@JsonProperty
@BeanProperty
var name:String = ""
@JsonProperty
@BeanProperty
var id:Long = 0
override def toString = name
override def equals(other: Any)= other match {
case other:User => other.id == this.id
case _ => false
}
}
In another class I get the sets of users and want to see the intersection.
val myFriends = friendService.getFriends("me")
val friendsFriends = friendService.getFriends("otheruser")
println(myFriends & friendsFriends)
以上代码不使用和印刷
Set()
然而,如果我用 for子手工操作套件,则我获得预期结果。
var matchedFriends:scala.collection.mutable.Set[User] = new HashSet[User]()
myFriends.foreach(myFriend => {
friendsFriends.foreach(myFriend => {
if(myFriend == myFriend){
matchedFriends.add(myFriend)
}
})
})
println(matchedFriends)
以上编码印刷
Set(Matt, Cass, Joe, Erin)
这部法律是公正的。
val set1 = Set(1, 2, 3, 4)
val set2 = Set(4,5,6,7,1)
println(set1 & set2)
以上印刷
Set(1, 4)
Do the set operations & &- etc.. only work on primitive objects ? Do I have to do something additional to my user object for this to work ?