[英]How to do the Scala's Seq ++ operand in Java
I'm working with Scala's Seq class in Java ( scala.collection.Seq<A>
) and according to Scala documentation , there a ++
operand that merges two Seq into one. 我正在使用Java中Scala的Seq类( scala.collection.Seq<A>
),根据Scala文档 ,有一个++
操作数将两个Seq合并为一个。 How can I do the same in Java? 如何在Java中做同样的事情?
Scala identifier ++
is translated into $plus$plus
on JVM. Scala标识符++
在JVM上转换为$plus$plus
。 Unfortunately, you can't call seq1.$plus$plus(seq2)
, because the real signature of ++
is 不幸的是,您不能调用seq1.$plus$plus(seq2)
,因为++
的真实签名是
def ++[B >: A, That](that: GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That
and supplying the CanBuildFrom
argument from Java is... technically possible, but not something you want to do. 并从Java提供CanBuildFrom
自变量在技术上是可行的,但您不希望这样做。
So I suggest converting Scala collections to Java using JavaConverters
methods before working with them in Java whenever CanBuildFrom
gets involved. 因此,我建议每当CanBuildFrom
涉及到Java时,在使用Java将它们转换为Java之前,先使用JavaConverters
方法将Scala集合转换为Java。
To do the same in java, you could use the addAll method. 要在Java中执行相同的操作,可以使用addAll方法。 Lets say you have two lists listOne and listTwo. 假设您有两个列表listOne和listTwo。
List<T> resultList = new ArrayList<T>(listOne);
resultList.addAll(listTwo);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.