I have two sets of objets and I want to get the intersection of the two sets. The objects in the sets look like this
@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)
The above code does not work and prints
Set()
However if I manually loop over the sets using foreach I get the desired result
var matchedFriends:scala.collection.mutable.Set[User] = new HashSet[User]()
myFriends.foreach(myFriend => {
friendsFriends.foreach(myFriend => {
if(myFriend == myFriend){
matchedFriends.add(myFriend)
}
})
})
println(matchedFriends)
the above code prints
Set(Matt, Cass, Joe, Erin)
This works just fine
val set1 = Set(1, 2, 3, 4)
val set2 = Set(4,5,6,7,1)
println(set1 & set2)
The above prints
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 ?
I'm not 100% positive about this, but I think your issue is caused by having implemented a custom
equalswithout a corresponding customhashCode. I'm sort of surprised your hash sets are working at all, actually...Your manual loop through the elements of each set works fine, of course, because you don't call
hashCodeat all :)