tuple也是不可变的,有句话形容tuple很合适,那就是 身有残疾的只读列表 。 tuple的创建 Copy valtuple =newTuple(1)valtuple2 =Tuple2(1,2)valtuple3 =Tuple3(1,2,3)// 最大支持到tuple22 tuple的遍历和取值 Copy // tuple.productIterator得到迭代器,进而遍
scala> val list01 = "hadoop"::"spark":: "storm"::Nillist01: List[String] = List(hadoop, spark, storm)// :: 操作符号是右结合的,所以上面的表达式和下面的等同scala> val list02 = "hadoop"::("spark":: ("storm"::Nil))list02: List[String] = List(hadoop, spark, storm)四、模式...
val v15=v6.map{x=>x+1}v6.map{_*1.0}v6.map{x=>x.toDouble}val v17=Array("hello world","hello hadoop")v17.map{_.split(" ")} 2、List链表 1.声明列表 底层用链表实现的数据结构。 定长列表:scala.collection.immutable.List,一旦声明之后长度不可变。 变长列表:scala.collection.mutable.Lis...
Scala在常用的集合的类别有数组,List,Set,Map,元祖。二、具体实现数组1、创建数组new Array[Int](10) 赋值:arr(0) = xxxArray[String](“s1”,”s2”,”s3”)1 2 3 4 5 6 7 8 9 10 11 12 13 14 /** * 创建数组两种方式: * 1.new Array[String](3) * 2.直接Array */ //创建类型为Int...
val tuples = List(("A", 1), ("B", 2), ("A", 3), ("B", 4)) val grouped = tuples.groupBy(_._1) 上述代码中,我们定义了一个包含多个元组的列表tuples,然后使用groupby函数根据元组中的第一个元素进行分组。最终,grouped变量将包含一个Map,其中key是元组中的第一个元素,value是具有相同key...
zipWithIndex方法将序列List[Int]转换成List[(Int, Int)],即List[Tuple2[Int, Int]]。Tuple2的第1个Int是元素,第2个Int是元素所处的位置。 List(2, 0, 1, 4, 12, 5).zipWithIndex.sorted.reverse.take(3) 输出:List((12,4), (5,5), (4,3)) ...
Tuple操作实战 队列操作实战 栈操作实战 mutable、immutable集合 以下内容来源于scala官方文档: http://www.scala-lang.org/docu/files/collections-api/collections.html Scala collections systematically distinguish between mutable and immutable collections. A mutable collection can be updated or ...
【Scala篇】--Scala中集合数组,list,set,map,元祖 一、前述 Scala在常用的集合的类别有数组,List,Set,Map,元祖。 二、具体实现 数组 1、创建数组 new Array[Int](10) 赋值:arr(0) = xxx Array[String](“s1”,”s2”,”s3”) /** * 创建数组两种方式:...
Let’s write out some unit tests to ensure this pattern match works as expected: it should "return empty Strings for Nil" in { ConvertListToTuple.unknownSizeToTuple(Nil) shouldBe ("", "") } "unknownSizeToTuple" should "convert list of 1 element to tuple1" in { ConvertListToTuple.un...
在scala 编程中经常需要用到各种数据结构,比如数组(Array)、元组(Tuple)、列表(List)、映射(Map)、集合(Set)等。 Scala同时支持可变集合和不可变集合,不可变集合从不可变,可以安全的并发访问; 不可变集合:scala.collection.immutable 可变集合: scala.collection.mutable ...