title: How does scala intersection & match elements in a set
body: <p>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 </p>
  
  <pre><code>@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 =&gt; other.id == this.id
        case _ =&gt; false
     }
  
  }
  </code></pre>
  
  <p>In another class I get the sets of users and want to see the intersection.  </p>
  
  <pre><code>val myFriends = friendService.getFriends("me")
  val friendsFriends = friendService.getFriends("otheruser")
  println(myFriends &amp; friendsFriends) 
  </code></pre>
  
  <p>The above code does not work and prints</p>
  
  <pre><code>Set()
  </code></pre>
  
  <p>However if I manually loop over the sets using foreach I get the desired result </p>
  
  <pre><code>var matchedFriends:scala.collection.mutable.Set[User] = new HashSet[User]()    
  myFriends.foreach(myFriend =&gt; {
    friendsFriends.foreach(myFriend =&gt; {
        if(myFriend == myFriend){
          matchedFriends.add(myFriend)
        }
    })
  })
  println(matchedFriends)
  </code></pre>
  
  <p>the above code prints </p>
  
  <pre><code>Set(Matt, Cass, Joe, Erin)
  </code></pre>
  
  <p>This works just fine  </p>
  
  <pre><code>val set1 = Set(1, 2, 3, 4)
  val set2 = Set(4,5,6,7,1)
  
  println(set1 &amp; set2)
  </code></pre>
  
  <p>The above prints  </p>
  
  <pre><code>Set(1, 4)
  </code></pre>
  
  <p>Do the set operations &amp; &amp;- etc.. only work on primitive objects ? 
  Do I have to do something additional to my user object for this to work ? </p>