考虑到List[Foo]
是这样的Java接口,将Seq[(String, String)]
转换为Foo
的最佳方法是什么:
public interface Foo {
Long getKey();
String getValue();
}
答案 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,""))