将List转换为Scala中属性元组的Seq?

时间:2013-07-25 20:43:06

标签: scala

考虑到List[Foo]是这样的Java接口,将Seq[(String, String)]转换为Foo的最佳方法是什么:

public interface Foo {
    Long getKey();
    String getValue();
}

1 个答案:

答案 0 :(得分:2)

您可以使用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,""))