English 中文(简体)
如何在一组内容中加固交叉点和对应部分
原标题:How does scala intersection & match elements in a set

我有两组喷气机,我想接上两组的交叉点。 这套装置中的物体就是这样。

@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) 

以上代码不使用和印刷

Set()

然而,如果我用 for子手工操作套件,则我获得预期结果。

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 ?

最佳回答

我对这一点不持100%的肯定态度,但我认为,你的问题在于没有相应的习俗()实施“条码”。 我感到惊讶的是,你的洗衣机完全在发挥作用,实际上......

当然,由于你没有打上<代码>hashCode,你通过每套作品的成分进行手工操作:

问题回答

From JavaDoc:

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

From Scala Doc:

Additionally, when overriding this method it is usually necessary to override hashCode to ensure that objects which are "equal" (o1.equals(o2) returns true) hash to the same Int. (o1.hashCode.equals(o2.hashCode)).

<代码>Set不适用,因为您打破了<代码>hashCode/code>。 页: 1

当优先于<代码>等于时,总是优先于<代码>hashCode/code>。





相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签