scala 交集 & 是如何实现的?匹配集合中的元素
我有两组对象,我想获得两组对象的交集。集合中的对象如下所示
@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)
。 &- 等..仅适用于原始对象? 我是否必须对我的用户对象做一些额外的事情才能使其工作?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对此并不是 100% 肯定,但我认为您的问题是由于实现了自定义
equals
而没有相应的自定义hashCode
引起的。实际上,我有点惊讶你的哈希集竟然能正常工作......当然,你对每个集合的元素的手动循环工作得很好,因为你根本不调用
hashCode
: )I'm not 100% positive about this, but I think your issue is caused by having implemented a custom
equals
without 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
hashCode
at all :)来自 JavaDoc:
来自 ScalaDoc:
Set
不起作用,因为您在覆盖equals
时破坏了hashCode
。From JavaDoc:
From ScalaDoc:
Set
is not working because you brokehashCode
when you overrodeequals
.当重写
equals
时,总是用它重写hashCode
。When overriding
equals
always overridehashCode
with it.