Mehran
Mehran

Reputation: 16821

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. How can I do the same in Java?

Upvotes: 1

Views: 243

Answers (2)

Alexey Romanov
Alexey Romanov

Reputation: 170713

Scala identifier ++ is translated into $plus$plus on JVM. Unfortunately, you can't call seq1.$plus$plus(seq2), because the real signature of ++ is

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.

So I suggest converting Scala collections to Java using JavaConverters methods before working with them in Java whenever CanBuildFrom gets involved.

Upvotes: 4

Chaitanya
Chaitanya

Reputation: 3638

To do the same in java, you could use the addAll method. Lets say you have two lists listOne and listTwo.

List<T> resultList = new ArrayList<T>(listOne);
resultList.addAll(listTwo);

Upvotes: 0

Related Questions