views:

118

answers:

2

Hey fellas,

What is a neat way to hold pairs of strings which are not necessarily key-values (might have duplicate keys), for a small collection? List[List[String]] works obviously but looks dirty.

Cheers
Parsa

+12  A: 

List[(String,String)] is the standard solution:

scala> List(("foo","bar"), ("foo","baz"))
res1: List[(java.lang.String, java.lang.String)] = List((foo,bar), (foo,baz))
larsmans
Exactly! The nice thing about tuples is that equality, pattern matching, etc. works as one would expect. `("foo", "bar") == ("foo", "bar")` returns `true`, for example.
soc
You also get lexicographic ordering of pairs, which is an extremely nice freebie.
Dave Griffith
I came across this nice tuple trick yesterday: http://goo.gl/MT6J. It makes a good use of the fact that all the tuple classes have instances of `Ordering` trait defined for them.
missingfaktor
+10  A: 

Tuples are the ideal data structure to represent pairs.

So use a list of (String, String) tuples.

sepp2k