我在查看scala的一个集合示例时注意到,当创建states3时,编译器更改了该集合成员的顺序,怀俄明州排在第三位,现在是最后一个。有人能解释一下为什么会发生这种事吗?
scala> val states = Set("Alabama", "Alaska", "Wyoming")
states: scala.collection.immutable.Set[String] = Set(Alabama, Alaska, Wyoming)
scala> val states2 = states + "Virginia"
s
对于三个映射a、b和c,我希望结果与其各自的原始映射的顺序相同。但是,如下所示,结果类似于b、a和c:
Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import collection.mutable
import collection.mutable
scala>
我有一个空格分隔的文本文件(temp.txt)如下:
a susan python
b rick java
c bella scala
我想把它读成一组,如下所示:
{'susan python', 'rick java', 'bella scala'}
我试过以下代码,但它只返回{'bella scala'}
temp_List = [];
with open('temp.txt', 'r') as f:
for line in f:
splitLine = line.s
我正在尝试将json解析到我的case类DealFormMap中
case class DealFormMap(limit: Option[Int], filter: Option[DealFormFilterMap])
case class DealFormFilterMap(date: Option[String], code: Option[String])
implicit val dealFormMapReads: Reads[DealFormMap] = (
(JsPath \ "limit").readNullable[Int] and
(JsP
scala
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_79).
Type in expressions to have them evaluated.
Type :help for more information.
scala> 300 to 1000 toSet
warning: there were 1 feature warning(s); re-run with -feature for details
res0: scala.collection.immuta
请考虑以下几点,它编译:
val f: String => Set[Integer] = ???
val a: Set[String] = ???
val b = a.flatMap(s => f(s))
现在,如果我按以下方式更改上面的第一行,代码将不再编译:
val f: String => Set[_ <: Integer] = ???
val a: Set[String] = ???
val b = a.flatMap(s => f(s))
错误如下:
/Foo.scala:31: error: no type parameters
for(elt <- bufferObject: scala.collection.mutable.Buffer)
// Do something with the element of the collection
按什么顺序访问for循环中的元素?随机的?
从Scala API中可以看到Buffer是Seq的一个子类,其中的元素是有序的。这也适用于上面的循环吗?
我正在尝试将Spark RDD保存为gzipped文本文件(或多个文本文件)到S3存储桶中。S3存储桶挂载到dbfs。我正在尝试使用以下命令保存该文件:
rddDataset.saveAsTextFile("/mnt/mymount/myfolder/")
但是当我尝试这样做的时候,我一直收到错误:
org.apache.spark.SparkException: Job aborted due to stage failure: Task 32 in stage 18.0 failed 4 times, most recent failure: Lost task 32.3
当我想读文件时,我的文件格式是:12334这个:23,word:21,老师:23
val fp = "/user/user_id.txt"
sc.textFile(fp).map { s =>
val Array(did, info_s) = s.split("\t")
val info = info_s.split(",").map { kv =>
val Array(k, v) = kv.split(":")
(k, v.toDouble)
}.toSeq
考虑使用+:向序列添加一个元素(从左边):
scala> val cl = Seq(1,2,3)
scala> 5 +: cl
res2: Seq[Int] = List(5, 1, 2, 3)
集合的等效性(如果有的话)是什么?
scala> val s = Set(1,2,3)
scala> 5 +: s
<console>:9: error: value +: is not a member of scala.collection.immutable.Set[Int]
5 +: s
scala> 5.toInt +
跟进。
如何重新排序
List(Map(d -> 4, a -> 12, c -> 30, b -> 2), Map(d -> 80, a -> 22, c -> 6666, b -> 442))
至
List(Map(d -> 4, b -> 2, a -> 12, c -> 30), Map(d -> 80, b -> 442, a -> 22, c -> 6666))
用Scala?
我有一个字符串">= 50.80",我正在尝试使用下面的拆分逻辑运算符和浮动值
val result = ">= 50.80"
val Pattern = "(<[=>]?|==|>=?|\\&\\&|\\|\\|)".r
val Pattern(operator) = result
println(operator)
错误:
线程"main“中的异常scala.MatchError:>= 50.80 (类java.lang.String).