0

I'm having some issues converting from Scala to Java, and then back to Scala. I'm trying to convert from a Scala mutable buffer to a Java List, and then back to a Scala mutable buffer after I apply Java's nifty shuffling function.

I tried using Scala's Random library's shuffling function (even when the buffer is converted to a Scala list) however it's not working for me, as the type of buffer is of type "Card" which is an Object type that I have set up for the project that I am working on. The code in question looks like this:

def shuffleDeck() {
  val list: java.util.List[Card] = cards
  val newList = java.util.Collections.shuffle(list)
  asScalaBuffer(newList)
}

in the Scala IDE I'm using, the error given to me is:

type mismatch; found : Unit required: java.util.List[?]

I'm not really sure what to do. Any and all help would be greatly appreciated!

2 Answers 2

1

The line that's causing the error is likely this one:

val newList = java.util.Collections.shuffle(list)

As Collections.shuffle(..) (in java.util) has return type void - the passed in list becomes shuffled, a new list is not created. With that in mind, your code should be:

def shuffleDeck() {
  val list: java.util.List[Card] = cards
  java.util.Collections.shuffle(list)
  asScalaBuffer(list)
}
Sign up to request clarification or add additional context in comments.

Comments

0

The java.util.Collections.shuffle will shuffle the list in place and it does not return a new list. The return type is void. Try calling asScalaBuffer with the original reference list.

Comments

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.