I have a value in Scala which is a Set
of tuples of Long
and Double
, and I have to assign the Long values to a new set to be able to look for its intersection with another set of Long values.
So my sets would be
firstSet : Set[(Long, Double)]
secondSet : Set[Long]
I want to put the Long
values of the firstSet
in a new set to apply the intersect method with secondSet
as argument.
Is it possible to do it in an efficient way? I'm very new to Scala so I don't just want to do a bunch of nested If statements.
CodePudding user response:
Use map
to get the first Long
value from each Tuple and create a new set. Note the elements in the Tuple are accesses with ._1
, ._2
and so on.
val firstSet = Set((0l,0.0), (1l, 1.0))
val firtSet: Set[(Long, Double)] = Set((0,0.0), (1,1.0))
firstSet.map(t => t._1)
val res5: Set[Long] = Set(0, 1)
Then you use this result to intersect with the other set