如何从 Java 列表中获取 Scala 列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6356465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:16:58  来源:igfitidea点击:

How to get Scala List from Java List?

javascala

提问by ace

I have a Java API that returns a List like:

我有一个 Java API,它返回一个列表,如:

public List<?> getByXPath(String xpathExpr)

I am using the below scala code:

我正在使用以下 Scala 代码:

val lst = node.getByXPath(xpath)

Now if I try scala syntax sugar like:

现在,如果我尝试像下面这样的 Scala 语法糖:

lst.foreach{ node => ... }

it does not work. I get the error:

这是行不通的。我收到错误:

value foreach is not a member of java.util.List[?0]

It seems I need to convert Java List to Scala List. How to do that in above context?

看来我需要将 Java 列表转换为 Scala 列表。在上述情况下如何做到这一点?

采纳答案by ace

EDIT: Note that this is deprecated since 2.12.0. Use JavaConvertersinstead. (comment by @Yaroslav)

编辑:请注意,自 2.12.0 以来已弃用。使用JavaConverters来代替。(@Yaroslav 的评论)

Since Scala 2.8this conversion is now built into the language using:

Scala 2.8 开始,这种转换现在使用以下方法内置到语言中:

import scala.collection.JavaConversions._

...

lst.toList.foreach{ node =>   .... }

works. asScaladid not work

作品。asScala不工作

回答by montreal.eric

There's a handy Scala object just for this - scala.collection.JavaConverters

有一个方便的 Scala 对象只是为了这个 - scala.collection.JavaConverters

You can do the import and asScalaafterwards as follows:

您可以asScala按如下方式进行导入和之后的操作:

import scala.collection.JavaConverters._

val lst = node.getByXPath(xpath).asScala
lst.foreach{ node =>   .... }

This should give you Scala's Bufferrepresentation allowing you to accomplish foreach.

这应该为您提供 Scala 的Buffer表示,允许您完成foreach.

回答by Radix

I was looking for an answer written in Java and surprisingly couldn't find any clean solutions here. After a while I was able to figure it out so I decided to add it here in case someone else is looking for the Java implementation (I guess it also works in Scala?):

我正在寻找用 Java 编写的答案,但令人惊讶的是在这里找不到任何干净的解决方案。过了一会儿,我弄清楚了,所以我决定在这里添加它,以防其他人正在寻找 Java 实现(我猜它也适用于 Scala?):

JavaConversions.asScalaBuffer(myJavaList).toList()

回答by Leandro carrasco

If you have to convert a Java List<ClassA>to a Scala List[ClassB], then you must do the following:

如果必须将 Java 转换为List<ClassA>Scala List[ClassB],则必须执行以下操作:

1) Add

1) 添加

import scala.collection.JavaConverters._

2) Use methods asScala, toListand then map

2)使用方法asScalatoList然后map

List <ClassA> javaList = ...
var scalaList[ClassB] = javaList.asScala.toList.map(x => new ClassB(x))

3) Add the following to the ClassBconstructor that receives ClassAas a parameter:

3)将以下内容添加到作为参数ClassB接收的构造ClassA函数中:

case class ClassB () {
?? def this (classA: ClassA) {
????? this (new ClassB (classA.getAttr1, ..., classA.getAttrN))
?? }
}

回答by user1553728

Since scala 2.8.1 use JavaConverters._to convert scala and Java collections using asScala and asJava methods.

由于 scala 2.8.1 用于JavaConverters._使用 asScala 和 asJava 方法转换 scala 和 Java 集合。

import scala.collection.JavaConverters._

import scala.collection.JavaConverters._

javalist.asScala

javalist.asScala

scalaSeq.asJava

scalaSeq.asJava

see the Conversion relationship scala doc site

请参阅转换关系scala doc 站点

回答by Xing-Wei Lin

Shortcut to convert java list to scala list

将java列表转换为scala列表的快捷方式

import scala.collection.JavaConverters._

import scala.collection.JavaConverters._

myjavaList.asScala.toList

myjavaList.asScala.toList