0

I am new to scala, How can i get all the indexs for particular string.

for example:

var taluk = List("Hyderabad", "Nampally", "Hyderabad" ,"Khairatabad")
taluk.indexOf("Hyderabad")

output is 0

But I want

output as 0,2

because there are two string match in vector.

DSpark
  • 3
  • 2

1 Answers1

1

One way to do this: zipWithIndex and then collect the indices for values matching yours:

scala> taluk.zipWithIndex.collect { case ("Hyderabad", i) => i }
res0: List[Int] = List(0, 2)
Tzach Zohar
  • 37,442
  • 3
  • 79
  • 85
  • Is there simple way to do this using find method? I mean to have general solution, including if the string not exists?? – Pavel Nov 23 '16 at 09:28
  • @PavelOliynyk I couldn't think of any, and neither could anyone answering the very similar question I've marked as possible duplicate (noticed it after I answered...) – Tzach Zohar Nov 23 '16 at 09:30