私は2組のオブジェクトを持っており、2組のオブジェクトの交点を取得したい。セット内のオブジェクトは次のようになります
@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
}
}
別のクラスで私はユーザーのセットを取得し、交差点を見たい。
val myFriends = friendService.getFriends("me")
val friendsFriends = friendService.getFriends("otheruser")
println(myFriends & friendsFriends)
上記のコードは機能せず、印刷します
Set()
しかし、手動でforeachを使用してセットをループすると、私は望ましい結果を得ます
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 ?