Teodorico Levoff Teodorico Levoff - 1 year ago 78
Scala Question

Most efficient way to search a nested list for a tuple in Scala

I want to search a nested list for a tuple but I want to know the most efficient way to do so in Scala. For example,

List[(String, Int)] = List((b,1), (c,1), (d,1))
say I want to search whether
exists in it. I could use contains, or find but in terms of efficiency what is the fastest way to do so?

Answer Source

List#contains is the fastest way to do it. It will make at most one pass of the List, and stops as soon as it finds a match. Unless you know the list is sorted, you can't make it any faster. The Scala collections library is written in such a way that all (if not, most) of the elementary functions are optimized for performance.

See the source.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download