Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
scala如何处理交叉点&;匹配集合中的元素_Scala_Scala Collections_Scala 2.9 - Fatal编程技术网

scala如何处理交叉点&;匹配集合中的元素

scala如何处理交叉点&;匹配集合中的元素,scala,scala-collections,scala-2.9,Scala,Scala Collections,Scala 2.9,我有两组对象,我想得到这两组对象的交点。集合中的对象如下所示 @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 { c

我有两组对象,我想得到这两组对象的交点。集合中的对象如下所示

@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)
执行设置操作&&-等。。只处理基本对象?
我是否必须对我的用户对象做一些额外的操作才能使其工作

当覆盖
equals
时,始终使用它覆盖
hashCode

对此我不是100%肯定,但我认为您的问题是由于实现了自定义
equals
而没有相应的自定义
hashCode
。我有点惊讶你的散列集居然能工作,事实上

当然,您可以手动循环每个集合的元素,因为您根本不需要从JavaDoc调用
hashCode

注意,通常需要重写hashCode方法 每当重写此方法时,以保持 hashCode方法的契约,它声明必须有相等的对象 具有相等的散列码

来自ScalaDoc:

此外,当重写此方法时,通常需要 重写哈希代码以确保“相等”的对象 (o1.equals(o2)将true)散列返回到相同的整数。 (o1.hashCode.equals(o2.hashCode))


Set
不起作用,因为您在重写
equals
时中断了
hashCode

myFriend==myFriend
真的吗?添加了哈希代码,由于mergeconflict,它起作用了,但在制定出一个更具规模的交集版本之前,如果有人感兴趣,它不会受到不变性的影响def有[A](set:set[A],target:A):Boolean={set.foldLeft(false){(rv,item)=>rv | | target==item}}}def union[A](set1:set[A],set2:set[A]):set[A]={set1.foldLeft(set[A]()){(union,item)=>{if(has(set2,item))union+item-else union}}