Search code examples
scalaindexoflinkedhashset

Looping through a LinkedHashSet using indexes in Scala


I have a LinkedHashSet which was created from a Seq. I used a LinkedHashSet because I need to keep the order of the Seq, but also ensure uniqueness, like a Set. I need to check this LinkedHashSet against another sequence to verify that various properties within them are the same. I assumed that I could loop through using an index, i, but it appears not. Here is an example of what I would like to accomplish.

var s: Seq[Int] = { 1 to mySeq.size }
return s.forall { i => 
  myLHS.indexOf(i).something == mySeq.indexOf(i).something &&
  myLHS.indexOf(i).somethingelse == mySeq.indexOf(i).somethingelse 
}

So how do I access individual elements of the LHS?


Solution

  • A LinkedHashSet is not necessary in this situation. Since I made it from a Seq, it is already ordered. I do not have to convert it to a LHS in order to also make it unique. Apparently, Seq has the distinct method which will remove duplicates from the sequence. From there, I can access the items via their indexes.