Search code examples
scalatuplesseq

Filtering a Seq of Tuple3 using one element of each Tuple


I have a Seq of Tuple3 elements. I want a resulting collection (probably a Set) made up with the second element of each tuple.

For example

(a, b, c), (d, e, f), (g, h, i) ==> (b, e, h)

Any idea? I searched a lot but all I'm finding has to do with filtering on the tuples, not within them, if that makes any sense.

I'm still quite new to Scala, learning is a long process :) Thanks for your help.


Solution

  • yourSeqOfTuples.map(tuple => tuple._2).toSet, which may be shortedned to yourSeqOfTuples.map(_._2).toSet

    You may use {} rather than () if you prefer it so. _2 is the method which gets the second element of the tuple.