0

I am trying to use Scala's immutable.Vector in Java. Can anyone post an simple Java code for this?

This is what I have tried till now.

  1. Tried Vector Builder.
  2. Tried Vector.concat(Seq<Traversable <A>>)

Here is a sample of what I have tried:

Vector<Long> part1= orignal.slice(0, indexOfMid);
Vector<Long> part2 = orignal.slice(indexOfMid, orignal.size());
orignal= part2.appendFront(1L);
Vector <Traversable<Long>> vectorOfTraversables = Vector.empty();
vectorOfTraversables.appendFront(orignal.toTraversable());
vectorOfTraversables.appendFront(part1.toTraversable());
orignal= Vector.concat(vectorOfTraversables.toSeq());

I am getting the following error

Exception in thread "main" java.lang.NoSuchMethodError:
scala.collection.immutable.Vector.concat(Lscala/collection/Seq;)Lscala/collection/immutable/Vector;

Is there a better totally different way to do this? Or am I going wrong somewhere?

5
  • You cannot change an immutable vector, so concat makes no sense. You need to construct a new vector entirely. Commented Jul 8, 2011 at 2:29
  • you mean i use a for loop again to create a new vector? that is if i cant use append at some point. is there some other way to do this? Commented Jul 8, 2011 at 2:34
  • The questions seems similar to: stackoverflow.com/questions/6591614/… Is that some kind of homework ? If yes, use the appropriate tag. Commented Jul 8, 2011 at 6:34
  • 2
    @paradigmatic, I can't imagine this is homework. Using Scala from Java for education purpose is just cruel. Commented Jul 8, 2011 at 8:34
  • use method patch(from: Int, that: GenSeq[A], replaced: Int): Vector[A] It's exactly what you need. Commented Nov 6, 2014 at 21:48

2 Answers 2

2

This should work:

Vector<Long> part1 = original.slice(0, indexOfMid);
Vector<Long> part2 = original.slice(indexOfMid, original.size());
VectorBuilder<Long> builder = new VectorBuilder<Long>();
builder.$plus$plus$eq(part1);
builder.$plus$eq(new Long(100));
builder.$plus$plus$eq(part2);
Vector<Long> result = builder.result();

VectorBuilder provides methods that do not use CanBuildFrom or repeated parameters, so it's much easier to use from Java.

By the way the NoSuchMethodError is probably due to the fact the concat is a method of the companion object. Import scala.collection.immutable.Vector$ and try this:

Vector$.MODULE$.concat(vectorOfTraversables.toSeq());
Sign up to request clarification or add additional context in comments.

2 Comments

I'd have used splitAt instead of slice. Also, why are you starting the slice at 1?
@Daniel, yep starting at 1 was a typo - now corrected. Fair point about splitAt, I just didn't feel like dealing with Scala tuples from the Java side.
1

If you see a NoSuchMethodError, or any other error that descends from LinkageError, it means that your code was compiled against a different version of a library than is in your classpath.

This is somewhat uncommon with Java libraries, but it's incredibly common in Scala, where binary compatibility is quite difficult to achieve—primarily because of traits.

Make sure your runtime classpath contains the exact same Scala version as your compile classpath, and you should at least get further.*

*EDIT: But, then again, you won't get very far without a LOT of pain. If you really need to use a Scala collection from Javaland, I think your best best is probably to write an interface in Java, and implement it from Scala.

1 Comment

Thanks Alex, but i just have one version of scala library on my machine. Any other clues? not to solve the problem but an alternate solution for my task?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.