0

What would be the best way to transform List[Foo] into Seq[(String, String)], considering that Foo is a Java interface like this:

public interface Foo {
    Long getKey();
    String getValue();
}
2
  • Where would the second String come from? Should Long be converted to String and be the first item in a tuple? Commented Mar 1, 2014 at 22:07
  • Exactly, @JacekLaskowski. Commented Mar 7, 2014 at 0:02

1 Answer 1

2

You can transform them with map.

class Bar extends Foo{
     | def getKey = 0
     | def getValue = ""
     | }
defined class Bar

scala> val bar = new Bar
bar: Bar = Bar@7fe69211       

scala> val foos = Seq(bar, bar, bar)
foos: Seq[Bar] = List(Bar@7fe69211, Bar@7fe69211, Bar@7fe69211)

scala> foos.map(foo => (foo.getKey.toString, foo.getValue))
res0: Seq[(String, String)] = List((0,""), (0,""), (0,""))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.