In scala.collection, there are two very similar objects JavaConversions and JavaConverters.
- What is the difference between these two objects?
- Why do they both exist?
- When do I want to use one vs. the other?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
EDIT:
Java Conversionsgot@deprecatedin Scala 2.13.0. Use scala.jdk.CollectionConverters instead.JavaConversionsprovide a series of implicit methods that convert between a Java collection and the closest corresponding Scala collection, and vice versa. This is done by creating wrappers that implement either the Scala interface and forward the calls to the underlying Java collection, or the Java interface, forwarding the calls to the underlying Scala collection.JavaConvertersuses the pimp-my-library pattern to “add” theasScalamethod to the Java collections and theasJavamethod to the Scala collections, which return the appropriate wrappers discussed above. It is newer (since version 2.8.1) thanJavaConversions(since 2.8) and makes the conversion between Scala and Java collection explicit. Contrary to what David writes in his answer, I’d recommend you make it a habit to useJavaConvertersas you’ll be much less likely to write code that makes a lot of implicit conversions, as you can control the only spot where that will happen: where you write.asScalaor.asJava.Here’s the conversion methods that
JavaConvertersprovide:To use the conversions directly from Java, though, you’re better off calling methods from
JavaConversionsdirectly; e.g.: