How to convert java.util.List[java.lang.Double] to Scala's List[Double]?












7














I'd like to convert a Java list of Java doubles (java.util.List[java.lang.Double]) to a Scala list of Scala doubles (List[Double]) in an efficient way.



Currently I'm mapping over the list converting each Double value to a Scala Double. I'd prefer not to map over every value and am looking for a more efficient way of doing this.



import collection.JavaConversions._
import collection.mutable.Buffer

val j: java.util.List[java.lang.Double] = Buffer(new java.lang.Double(1.0), new java.lang.Double(2.0))
val s: List[Double] = ...


I've seen documentation to go from Scala --> Java, but not much going the other way.










share|improve this question
























  • "I've seen documentation to go from Scala --> Java, but not much going the other way."... did you now? Care to share that information? ;-)
    – Make42
    Oct 19 '17 at 12:12










  • I found out the answer: stackoverflow.com/q/46830217/4533188
    – Make42
    Oct 19 '17 at 12:48
















7














I'd like to convert a Java list of Java doubles (java.util.List[java.lang.Double]) to a Scala list of Scala doubles (List[Double]) in an efficient way.



Currently I'm mapping over the list converting each Double value to a Scala Double. I'd prefer not to map over every value and am looking for a more efficient way of doing this.



import collection.JavaConversions._
import collection.mutable.Buffer

val j: java.util.List[java.lang.Double] = Buffer(new java.lang.Double(1.0), new java.lang.Double(2.0))
val s: List[Double] = ...


I've seen documentation to go from Scala --> Java, but not much going the other way.










share|improve this question
























  • "I've seen documentation to go from Scala --> Java, but not much going the other way."... did you now? Care to share that information? ;-)
    – Make42
    Oct 19 '17 at 12:12










  • I found out the answer: stackoverflow.com/q/46830217/4533188
    – Make42
    Oct 19 '17 at 12:48














7












7








7


1





I'd like to convert a Java list of Java doubles (java.util.List[java.lang.Double]) to a Scala list of Scala doubles (List[Double]) in an efficient way.



Currently I'm mapping over the list converting each Double value to a Scala Double. I'd prefer not to map over every value and am looking for a more efficient way of doing this.



import collection.JavaConversions._
import collection.mutable.Buffer

val j: java.util.List[java.lang.Double] = Buffer(new java.lang.Double(1.0), new java.lang.Double(2.0))
val s: List[Double] = ...


I've seen documentation to go from Scala --> Java, but not much going the other way.










share|improve this question















I'd like to convert a Java list of Java doubles (java.util.List[java.lang.Double]) to a Scala list of Scala doubles (List[Double]) in an efficient way.



Currently I'm mapping over the list converting each Double value to a Scala Double. I'd prefer not to map over every value and am looking for a more efficient way of doing this.



import collection.JavaConversions._
import collection.mutable.Buffer

val j: java.util.List[java.lang.Double] = Buffer(new java.lang.Double(1.0), new java.lang.Double(2.0))
val s: List[Double] = ...


I've seen documentation to go from Scala --> Java, but not much going the other way.







java scala implicit-conversion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 1 '15 at 9:56









Jacek Laskowski

43.2k17126256




43.2k17126256










asked May 23 '14 at 5:30









Erik Shilts

2,75321940




2,75321940












  • "I've seen documentation to go from Scala --> Java, but not much going the other way."... did you now? Care to share that information? ;-)
    – Make42
    Oct 19 '17 at 12:12










  • I found out the answer: stackoverflow.com/q/46830217/4533188
    – Make42
    Oct 19 '17 at 12:48


















  • "I've seen documentation to go from Scala --> Java, but not much going the other way."... did you now? Care to share that information? ;-)
    – Make42
    Oct 19 '17 at 12:12










  • I found out the answer: stackoverflow.com/q/46830217/4533188
    – Make42
    Oct 19 '17 at 12:48
















"I've seen documentation to go from Scala --> Java, but not much going the other way."... did you now? Care to share that information? ;-)
– Make42
Oct 19 '17 at 12:12




"I've seen documentation to go from Scala --> Java, but not much going the other way."... did you now? Care to share that information? ;-)
– Make42
Oct 19 '17 at 12:12












I found out the answer: stackoverflow.com/q/46830217/4533188
– Make42
Oct 19 '17 at 12:48




I found out the answer: stackoverflow.com/q/46830217/4533188
– Make42
Oct 19 '17 at 12:48












2 Answers
2






active

oldest

votes


















18














The recommendation is to use JavaConverters, not JavaConversions:



import collection.JavaConverters._
import scala.collection.breakOut
val l: List[Double] = j.asScala.map(_.doubleValue)(breakOut)


doubleValue will convert it from a java.lang.Double into a scala.Double. breakOut tells it to produce a List as the result of the map, without having to traverse it another time to convert to a List. You could also just do toList instead of breakOut if you didn't care about the extra traversal.



The Java classes are entirely different objects from the Scala ones; it's not just a name-change. Thus, it would be impossible to convert without traversing. You have to create an entirely new collection and then look at each element in order to (convert it and) move it over.



The reason the types are different is that java.lang.Double is a "boxed primitive" whereas scala.Double is equivalent to Java's double primitive. The conversion here is essentially "unboxing" the primitive.






share|improve this answer























  • Is there a way to do it without any traversals? Perhaps that's not possible, but I naively imagine that casting won't involve any traversals.
    – Erik Shilts
    May 23 '14 at 5:47










  • Note that if you are willing to accept a Seq rather than a List precisely, you can avoid the traversal by converting the Scala-converted Java List into a view by calling the view method before calling map. That will give you a lazily evaluated SeqView that will apply the conversions to the elements on an as-needed basis.
    – reggert
    May 24 '14 at 17:59





















0














import collection.JavaConverters._

val javaList : java.util.List[Double] = ...
val scalaList : List[Double] = j.asScala.toList





share|improve this answer





















  • This is able to cast a java.util.list[Double] to a List[Double], but the tricky part is also converting from java.lang.Double to Scala Double.
    – Erik Shilts
    May 23 '14 at 5:48






  • 1




    @Erik, It's not a cast: asScala traverses the java-based collection and builds a brand new scala-based collection. It has to since the classes are completely different.
    – dhg
    May 23 '14 at 5:57










  • @dhg, you're in fact wrong. asScala does not traverse a collection; asScala creates a view into the original collection. It's toList which performs actual traversal. See scala.collection.convert.Wrappers trait: it contains implementations of these views, and it is obvious that they only forward collection operations to the original Java collections.
    – Vladimir Matveev
    May 23 '14 at 7:34












  • @dhg, you're right, however, that javaList.asScala.map(_.doubleValue).toList will perform two traversals, the first one happening in map, and the second in toList.
    – Vladimir Matveev
    May 23 '14 at 7:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23821576%2fhow-to-convert-java-util-listjava-lang-double-to-scalas-listdouble%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









18














The recommendation is to use JavaConverters, not JavaConversions:



import collection.JavaConverters._
import scala.collection.breakOut
val l: List[Double] = j.asScala.map(_.doubleValue)(breakOut)


doubleValue will convert it from a java.lang.Double into a scala.Double. breakOut tells it to produce a List as the result of the map, without having to traverse it another time to convert to a List. You could also just do toList instead of breakOut if you didn't care about the extra traversal.



The Java classes are entirely different objects from the Scala ones; it's not just a name-change. Thus, it would be impossible to convert without traversing. You have to create an entirely new collection and then look at each element in order to (convert it and) move it over.



The reason the types are different is that java.lang.Double is a "boxed primitive" whereas scala.Double is equivalent to Java's double primitive. The conversion here is essentially "unboxing" the primitive.






share|improve this answer























  • Is there a way to do it without any traversals? Perhaps that's not possible, but I naively imagine that casting won't involve any traversals.
    – Erik Shilts
    May 23 '14 at 5:47










  • Note that if you are willing to accept a Seq rather than a List precisely, you can avoid the traversal by converting the Scala-converted Java List into a view by calling the view method before calling map. That will give you a lazily evaluated SeqView that will apply the conversions to the elements on an as-needed basis.
    – reggert
    May 24 '14 at 17:59


















18














The recommendation is to use JavaConverters, not JavaConversions:



import collection.JavaConverters._
import scala.collection.breakOut
val l: List[Double] = j.asScala.map(_.doubleValue)(breakOut)


doubleValue will convert it from a java.lang.Double into a scala.Double. breakOut tells it to produce a List as the result of the map, without having to traverse it another time to convert to a List. You could also just do toList instead of breakOut if you didn't care about the extra traversal.



The Java classes are entirely different objects from the Scala ones; it's not just a name-change. Thus, it would be impossible to convert without traversing. You have to create an entirely new collection and then look at each element in order to (convert it and) move it over.



The reason the types are different is that java.lang.Double is a "boxed primitive" whereas scala.Double is equivalent to Java's double primitive. The conversion here is essentially "unboxing" the primitive.






share|improve this answer























  • Is there a way to do it without any traversals? Perhaps that's not possible, but I naively imagine that casting won't involve any traversals.
    – Erik Shilts
    May 23 '14 at 5:47










  • Note that if you are willing to accept a Seq rather than a List precisely, you can avoid the traversal by converting the Scala-converted Java List into a view by calling the view method before calling map. That will give you a lazily evaluated SeqView that will apply the conversions to the elements on an as-needed basis.
    – reggert
    May 24 '14 at 17:59
















18












18








18






The recommendation is to use JavaConverters, not JavaConversions:



import collection.JavaConverters._
import scala.collection.breakOut
val l: List[Double] = j.asScala.map(_.doubleValue)(breakOut)


doubleValue will convert it from a java.lang.Double into a scala.Double. breakOut tells it to produce a List as the result of the map, without having to traverse it another time to convert to a List. You could also just do toList instead of breakOut if you didn't care about the extra traversal.



The Java classes are entirely different objects from the Scala ones; it's not just a name-change. Thus, it would be impossible to convert without traversing. You have to create an entirely new collection and then look at each element in order to (convert it and) move it over.



The reason the types are different is that java.lang.Double is a "boxed primitive" whereas scala.Double is equivalent to Java's double primitive. The conversion here is essentially "unboxing" the primitive.






share|improve this answer














The recommendation is to use JavaConverters, not JavaConversions:



import collection.JavaConverters._
import scala.collection.breakOut
val l: List[Double] = j.asScala.map(_.doubleValue)(breakOut)


doubleValue will convert it from a java.lang.Double into a scala.Double. breakOut tells it to produce a List as the result of the map, without having to traverse it another time to convert to a List. You could also just do toList instead of breakOut if you didn't care about the extra traversal.



The Java classes are entirely different objects from the Scala ones; it's not just a name-change. Thus, it would be impossible to convert without traversing. You have to create an entirely new collection and then look at each element in order to (convert it and) move it over.



The reason the types are different is that java.lang.Double is a "boxed primitive" whereas scala.Double is equivalent to Java's double primitive. The conversion here is essentially "unboxing" the primitive.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 1 '15 at 9:57









Jacek Laskowski

43.2k17126256




43.2k17126256










answered May 23 '14 at 5:36









dhg

45.2k698131




45.2k698131












  • Is there a way to do it without any traversals? Perhaps that's not possible, but I naively imagine that casting won't involve any traversals.
    – Erik Shilts
    May 23 '14 at 5:47










  • Note that if you are willing to accept a Seq rather than a List precisely, you can avoid the traversal by converting the Scala-converted Java List into a view by calling the view method before calling map. That will give you a lazily evaluated SeqView that will apply the conversions to the elements on an as-needed basis.
    – reggert
    May 24 '14 at 17:59




















  • Is there a way to do it without any traversals? Perhaps that's not possible, but I naively imagine that casting won't involve any traversals.
    – Erik Shilts
    May 23 '14 at 5:47










  • Note that if you are willing to accept a Seq rather than a List precisely, you can avoid the traversal by converting the Scala-converted Java List into a view by calling the view method before calling map. That will give you a lazily evaluated SeqView that will apply the conversions to the elements on an as-needed basis.
    – reggert
    May 24 '14 at 17:59


















Is there a way to do it without any traversals? Perhaps that's not possible, but I naively imagine that casting won't involve any traversals.
– Erik Shilts
May 23 '14 at 5:47




Is there a way to do it without any traversals? Perhaps that's not possible, but I naively imagine that casting won't involve any traversals.
– Erik Shilts
May 23 '14 at 5:47












Note that if you are willing to accept a Seq rather than a List precisely, you can avoid the traversal by converting the Scala-converted Java List into a view by calling the view method before calling map. That will give you a lazily evaluated SeqView that will apply the conversions to the elements on an as-needed basis.
– reggert
May 24 '14 at 17:59






Note that if you are willing to accept a Seq rather than a List precisely, you can avoid the traversal by converting the Scala-converted Java List into a view by calling the view method before calling map. That will give you a lazily evaluated SeqView that will apply the conversions to the elements on an as-needed basis.
– reggert
May 24 '14 at 17:59















0














import collection.JavaConverters._

val javaList : java.util.List[Double] = ...
val scalaList : List[Double] = j.asScala.toList





share|improve this answer





















  • This is able to cast a java.util.list[Double] to a List[Double], but the tricky part is also converting from java.lang.Double to Scala Double.
    – Erik Shilts
    May 23 '14 at 5:48






  • 1




    @Erik, It's not a cast: asScala traverses the java-based collection and builds a brand new scala-based collection. It has to since the classes are completely different.
    – dhg
    May 23 '14 at 5:57










  • @dhg, you're in fact wrong. asScala does not traverse a collection; asScala creates a view into the original collection. It's toList which performs actual traversal. See scala.collection.convert.Wrappers trait: it contains implementations of these views, and it is obvious that they only forward collection operations to the original Java collections.
    – Vladimir Matveev
    May 23 '14 at 7:34












  • @dhg, you're right, however, that javaList.asScala.map(_.doubleValue).toList will perform two traversals, the first one happening in map, and the second in toList.
    – Vladimir Matveev
    May 23 '14 at 7:41
















0














import collection.JavaConverters._

val javaList : java.util.List[Double] = ...
val scalaList : List[Double] = j.asScala.toList





share|improve this answer





















  • This is able to cast a java.util.list[Double] to a List[Double], but the tricky part is also converting from java.lang.Double to Scala Double.
    – Erik Shilts
    May 23 '14 at 5:48






  • 1




    @Erik, It's not a cast: asScala traverses the java-based collection and builds a brand new scala-based collection. It has to since the classes are completely different.
    – dhg
    May 23 '14 at 5:57










  • @dhg, you're in fact wrong. asScala does not traverse a collection; asScala creates a view into the original collection. It's toList which performs actual traversal. See scala.collection.convert.Wrappers trait: it contains implementations of these views, and it is obvious that they only forward collection operations to the original Java collections.
    – Vladimir Matveev
    May 23 '14 at 7:34












  • @dhg, you're right, however, that javaList.asScala.map(_.doubleValue).toList will perform two traversals, the first one happening in map, and the second in toList.
    – Vladimir Matveev
    May 23 '14 at 7:41














0












0








0






import collection.JavaConverters._

val javaList : java.util.List[Double] = ...
val scalaList : List[Double] = j.asScala.toList





share|improve this answer












import collection.JavaConverters._

val javaList : java.util.List[Double] = ...
val scalaList : List[Double] = j.asScala.toList






share|improve this answer












share|improve this answer



share|improve this answer










answered May 23 '14 at 5:36









ggreiner

9,04213749




9,04213749












  • This is able to cast a java.util.list[Double] to a List[Double], but the tricky part is also converting from java.lang.Double to Scala Double.
    – Erik Shilts
    May 23 '14 at 5:48






  • 1




    @Erik, It's not a cast: asScala traverses the java-based collection and builds a brand new scala-based collection. It has to since the classes are completely different.
    – dhg
    May 23 '14 at 5:57










  • @dhg, you're in fact wrong. asScala does not traverse a collection; asScala creates a view into the original collection. It's toList which performs actual traversal. See scala.collection.convert.Wrappers trait: it contains implementations of these views, and it is obvious that they only forward collection operations to the original Java collections.
    – Vladimir Matveev
    May 23 '14 at 7:34












  • @dhg, you're right, however, that javaList.asScala.map(_.doubleValue).toList will perform two traversals, the first one happening in map, and the second in toList.
    – Vladimir Matveev
    May 23 '14 at 7:41


















  • This is able to cast a java.util.list[Double] to a List[Double], but the tricky part is also converting from java.lang.Double to Scala Double.
    – Erik Shilts
    May 23 '14 at 5:48






  • 1




    @Erik, It's not a cast: asScala traverses the java-based collection and builds a brand new scala-based collection. It has to since the classes are completely different.
    – dhg
    May 23 '14 at 5:57










  • @dhg, you're in fact wrong. asScala does not traverse a collection; asScala creates a view into the original collection. It's toList which performs actual traversal. See scala.collection.convert.Wrappers trait: it contains implementations of these views, and it is obvious that they only forward collection operations to the original Java collections.
    – Vladimir Matveev
    May 23 '14 at 7:34












  • @dhg, you're right, however, that javaList.asScala.map(_.doubleValue).toList will perform two traversals, the first one happening in map, and the second in toList.
    – Vladimir Matveev
    May 23 '14 at 7:41
















This is able to cast a java.util.list[Double] to a List[Double], but the tricky part is also converting from java.lang.Double to Scala Double.
– Erik Shilts
May 23 '14 at 5:48




This is able to cast a java.util.list[Double] to a List[Double], but the tricky part is also converting from java.lang.Double to Scala Double.
– Erik Shilts
May 23 '14 at 5:48




1




1




@Erik, It's not a cast: asScala traverses the java-based collection and builds a brand new scala-based collection. It has to since the classes are completely different.
– dhg
May 23 '14 at 5:57




@Erik, It's not a cast: asScala traverses the java-based collection and builds a brand new scala-based collection. It has to since the classes are completely different.
– dhg
May 23 '14 at 5:57












@dhg, you're in fact wrong. asScala does not traverse a collection; asScala creates a view into the original collection. It's toList which performs actual traversal. See scala.collection.convert.Wrappers trait: it contains implementations of these views, and it is obvious that they only forward collection operations to the original Java collections.
– Vladimir Matveev
May 23 '14 at 7:34






@dhg, you're in fact wrong. asScala does not traverse a collection; asScala creates a view into the original collection. It's toList which performs actual traversal. See scala.collection.convert.Wrappers trait: it contains implementations of these views, and it is obvious that they only forward collection operations to the original Java collections.
– Vladimir Matveev
May 23 '14 at 7:34














@dhg, you're right, however, that javaList.asScala.map(_.doubleValue).toList will perform two traversals, the first one happening in map, and the second in toList.
– Vladimir Matveev
May 23 '14 at 7:41




@dhg, you're right, however, that javaList.asScala.map(_.doubleValue).toList will perform two traversals, the first one happening in map, and the second in toList.
– Vladimir Matveev
May 23 '14 at 7:41


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23821576%2fhow-to-convert-java-util-listjava-lang-double-to-scalas-listdouble%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]