Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala convert Iterable of Tuples to Array of Just Tuple._2

Tags:

scala

Say I have a Iterable[(Int, String)]. How do I get an array of just the "values"? That is, how do I convert from Iterable[(Int, String)] => Array[String]? The "keys" or "values" do not have to be unique, and that's why I put them in quotation marks.

like image 420
user3685285 Avatar asked Mar 09 '23 04:03

user3685285


2 Answers

iterable.map(_._2).toArray

_._2 : take out the second element of the tuple represented by input variable( _ ) whose name I don't care.

like image 81
Kshitij Banerjee Avatar answered Mar 12 '23 04:03

Kshitij Banerjee


Simply:

val iterable: Iterable[(Int, String)] = Iterable((1, "a"), (2, "b"))

val values = iterable.toArray.map(_._2)
like image 43
Tanjin Avatar answered Mar 12 '23 05:03

Tanjin