scala-collections

Fetch all the duplicate records from a list without quadratic time complexity?

馋奶兔 提交于 2020-01-06 05:44:13
问题 Below is my list which contains the column names: country, gender & age. scala> funList res1: List[(String, String, String)] = List((india,M,15), (usa,F,25), (australia,M,35), (kenya,M,55), (russia,M,75), (china,T,95), (england,F,65), (germany,F,25), (finland,M,45), (australia,F,35)) My goal is to find the duplicate records with the combination of (country,age). Please note that I want to only fetch all the duplicate records and ignore others. And list should also contain other column values

Why Scala 'String' object's iterator and 'List[Int]' object's iterator are behaving differently here?

不问归期 提交于 2020-01-04 13:44:11
问题 I just wanted to explore the behaviors of the iterators of a String object and a List[Int] object in REPL and the tests are as shown below: scala> val list = List(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) scala> val itL = list.iterator itL: Iterator[Int] = non-empty iterator scala> List(8,5,list.size-13).map(itL.take(_).mkString) res85: List[String] = List(12345678, 910111213, 141516) scala> val st = "abcdefghijklmnop

Scala SortedMap : Get all keys greater than a given key

妖精的绣舞 提交于 2020-01-04 04:48:46
问题 Given a Scala collection.SortedMap and a key k , what is the most efficient way of getting all keys (or even better, all key-value pairs ) greater than k stored in the sorted map. The returned set of keys should preserve the order of keys. Of course, I would like to avoid to peruse the whole data structure (i.e. using filterKeys ), and take advantage of the fact that the map is sorted. I would like to do something like : val m = collection.SortedMap((1,1) -> "somevalue", (1,2) -> "somevalue",

Unsigned integer in scala

只谈情不闲聊 提交于 2020-01-03 05:22:31
问题 I have a requirement to store only positive values. As I understand, signed can store both positive and negative values. Are there any unsigned integer, double data types in Scala? Regards 回答1: There was a proposal to include new data types for unsigned Int in scala, but if was going to have a performance impact. Hence the people who maintain scala decided not to go ahead with the proposal of an unsigned Int in scala. Please refer the following https://docs.scala-lang.org/sips/rejected

Split a scala list into n interleaving lists

懵懂的女人 提交于 2020-01-02 04:25:13
问题 Given a List such as List(1, 2, 3, 4, 5, 6, 7) what is the best way to split it into n sub-lists, putting items into each list in a round-robin fashion? e.g. if n = 3, the result should be List(List(1, 4, 7), List(2, 5), List(3, 6)) I thought there would be a method in the collections API to do this, but I can't seem to find it. Bonus points for classy one-liners ;) 回答1: scala> def round[T](l: List[T], n: Int) = (0 until n).map{ i => l.drop(i).sliding(1, n).flatten.toList }.toList round: [T]

How to make Scala's immutable collections hold immutable objects

别说谁变了你拦得住时间么 提交于 2020-01-02 03:54:06
问题 I'm evaluating Scala and am having a problem with its immutable collections. I want to make immutable collections, which are completely immutable, right down through all the contained objects, the objects they reference, ad infinitum. Is there a simple way to do this? The code on http://www.finalcog.com/immutable-containers-scala illustrates what I'm trying to achieve, and a nasty work around (ImmutablePoint). The problem with the workaround is that every time I want to change an object I

Find and Replace item in Scala collection

旧巷老猫 提交于 2020-01-02 01:30:30
问题 Let's say I have a list: val list = List(1,2,3,4,5) I want to replace all/first items that satisfy a predicate, I know the following way to do it: (e.g. replace any number that is even with -1) val filteredList = list.zipWithIndex.filter(_._2 % 2 == 0) val onlyFirst = list.updated(filteredList.head._2, -1) val all = for(i <- list) yield if(i % 2 ==0) -1 else i Is there any collection function or nice Scala way that helps in this situation and has a good performance ? I also want to keep the

Convert java.util.stream.Stream to Scala Stream

非 Y 不嫁゛ 提交于 2020-01-01 14:23:21
问题 I know how I can use the Java libraries, and I can write some loops that do the stuff I need for me, but the question is more, why is there nothing in scala.collection.JavaConverters or scala.collection.JavaConverstions to convert a java.util.stream.Stream to a scala.collection.immutable.Stream ? I would like to do something like this: def streamFiles(path: Path): Stream[Path] = { Files.newDirectoryStream(path).asScala } But instead I have to write something like this: def streamFiles(path:

Convert java.util.stream.Stream to Scala Stream

别等时光非礼了梦想. 提交于 2020-01-01 14:20:20
问题 I know how I can use the Java libraries, and I can write some loops that do the stuff I need for me, but the question is more, why is there nothing in scala.collection.JavaConverters or scala.collection.JavaConverstions to convert a java.util.stream.Stream to a scala.collection.immutable.Stream ? I would like to do something like this: def streamFiles(path: Path): Stream[Path] = { Files.newDirectoryStream(path).asScala } But instead I have to write something like this: def streamFiles(path:

In Scala, how to check if a Map contains all entries from another Map?

烂漫一生 提交于 2020-01-01 08:22:11
问题 Total newb question. Say I have 2 maps val map1 = Map("ram"->"2gb", "size"->"15", "color"->"red", "fruit"->"strawberry") val map2 = Map("ram"->"2gb", "size"->"15", "color"->"red") and I want to know if map1 fully contains map2 (extra key/values in map1 are okay), what's a good Scala way to do that? The best I could come up with was to create my own function: def doesMapContainMap(map1: Map[_,_], map2: Map[_,_]): Boolean = { var matchCount: Int = 0 map2 foreach { entry => { if (map1.exists(x =