scala-collections

Converting a Java collection into a Scala collection

假如想象 提交于 2019-12-28 01:42:32
问题 Related to Stack Overflow question Scala equivalent of new HashSet(Collection) , how do I convert a Java collection ( java.util.List say) into a Scala collection List ? I am actually trying to convert a Java API call to Spring's SimpleJdbcTemplate , which returns a java.util.List<T> , into a Scala immutable HashSet . So for example: val l: java.util.List[String] = javaApi.query( ... ) val s: HashSet[String] = //make a set from l This seems to work. Criticism is welcome! import scala

scala convert List(Some()) to List(string)

烂漫一生 提交于 2019-12-25 04:19:35
问题 how to convert a scala list of some types to list of strings Ex : List(Some(1234), Some(2345), Some(45678)) to List("1234","2345","45678") 回答1: You can go for this: scala> List(Some(1234), Some(2345), Some(45678)).flatten.map(x => x.toString) res11: List[String] = List(1234, 2345, 45678) 回答2: You can, as already suggested, flatten the collection and then map the toString method over its items, but you can achieve the same result in one pass by using collect : val in = List(Some(1234), Some

In Scala, how can I merge maps as below?

我只是一个虾纸丫 提交于 2019-12-25 03:59:26
问题 I would like to know how I can merge maps in Scala. val prevMap = Map(1-> Set("a"), 2-> Set("b")) val newMap = Map(2-> Set("a","b"),1-> Set("c")) val expected = Map(1->Set("a","c"), 2-> Set("a","b")) Basically, expected map is newMap + add all values that had a different key between prev and new Thanks 回答1: Using the standard library, like this, m1 ++ m2.map { case (k,v) => k -> (v ++ m1.getOrElse(k,Set())) } Consider the first occurrence of ++ , for appending maps. The key value pairs of the

How to chain Iterable without computing underlying iterators

时光毁灭记忆、已成空白 提交于 2019-12-24 09:52:38
问题 If I had two iterators I could just write iter1 ++ iter2 and iterators would not be computed until they are needed. Is there any way to chain Iterable instances in the same way? I tried to use iterable1 ++ iterable2 but it causes immediately calculating nested values just like they are added to some structure. Is it possible to avoid this extra calculations and creating extra data structures? 回答1: No. Iterable is just an interface that can be implemented by anything that can be iterated over.

Understanding Scala -> syntax

喜夏-厌秋 提交于 2019-12-24 03:19:10
问题 I am getting a taste of Scala through the artima "Programming in Scala" book. While presenting the Map traits, the authors go to some lengths to describe the -> syntax as a method that can be applied to any type to get a tuple. And indeed: scala> (2->"two") res1: (Int, String) = (2,two) scala> (2,"two") res2: (Int, String) = (2,two) scala> (2->"two") == (2, "two") res3: Boolean = true But those are not equivalent: scala> Map(1->"one") + (2->"two") res4: scala.collection.immutable.Map[Int

Scala map() on a Map[..] much slower than mapValues()

泄露秘密 提交于 2019-12-23 23:41:10
问题 In a Scala program I wrote I have a scala.collection.Map that maps a String to some calculated values (in detail it's Map[String, (Double, immutable.Map[String, Double], Double)] - I know that's ugly and should (and will be) wrapped). Now, if I do this: stats.map { case(c, (prior, pwc, denom)) => { println(c) ... } } it takes about 30 seconds to print out roughly 50 times a value of c ! The println is just a test statement - the actual calculation I need was even slower (I aborted after 1

Scala Mutable Option?

一曲冷凌霜 提交于 2019-12-23 11:47:43
问题 I want something like this: private val cachedResponse = mutable.Option.empty[A] def get: A = cachedResponse getOrElseUpdate db.findModel() def update: Unit = { db.updateModel cachedResponse.empty() // set it to None/Option.empty } I am not looking for a generic HashMap based memoization like this. I tried implementing it using a var Option[A] but it did not look very idiomatic to me: private var cachedResponse: Option[A] = None def get: A = cachedResponse getOrElse { cachedResponse = Option

Use cases of Scala collection forwarders and proxies

好久不见. 提交于 2019-12-23 09:26:48
问题 Scala's collection library contains the forwarders IterableForwarder, TraversableForwarder, SeqForwarder and proxies like IterableProxy, MapProxy, SeqProxy, SetProxy, TraversableProxy, etc. Forwarders and proxies both delegate collection methods to an underlying collection object. The main difference between these two are that forwarders don't forward calls that would create new collection objects of the same kind. In which cases would I prefer one of these types over the other? Why and when

Calling Scala Monads from Java #map

我怕爱的太早我们不能终老 提交于 2019-12-23 08:33:11
问题 I have an instance of a scala.collection.immutable.List and I want to call the map method on it, BUT from Java. I need to supply a CanBuildFrom . I noticed that a lot of the scala collections companion objects contain implicit CanBuildFrom instances, but I cannot work out which one I need to use. Here is my Java code: Function1<WeatherData, BigDecimal> mapper = new AbstractFunction1<WeatherData, BigDecimal>(){ @Override public BigDecimal apply(WeatherData data) { return data.getTemps().reduce

Extract a specific JSON structure from a json string in a Spark Rdd - Scala

随声附和 提交于 2019-12-23 04:22:42
问题 I have a json string such as: {"sequence":89,"id":8697344444103393,"trackingInfo":{"location":"Browse","row":0,"trackId":14170286,"listId":"cd7c2c7a-00f6-4035-867f-d1dd7d89972d_6625365X3XX1505943605585","videoId":80000778,"rank":0,"requestId":"ac12f4e1-5644-46af-87d1-ec3b92ce4896-4071171"},"type":["Play","Action","Session"],"time":527636408955},1], {"sequence":155,"id":8697389381205360,"trackingInfo":{"location":"Browse","row":0,"trackId":14170286,"listId":"cd7c2c7a-00f6-4035-867f