美文网首页
映射、元组、集合

映射、元组、集合

作者: lehuai | 来源:发表于2018-01-03 09:58 被阅读0次

映射

scala> val map1 = Map ( "scala" -> 1,"java" ->2,"python" -> 3)
map1: scala.collection.immutable.Map[String,Int] = Map(scala -> 1, java -> 2, python -> 3)
scala> val map2 = Map(("scala",1),("java",2),("python",3))
map2: scala.collection.immutable.Map[String,Int] = Map(scala -> 1, java -> 2, python -> 3)
scala> map1("scala")
res18: Int = 1
scala> import scala.collection.mutable._
import scala.collection.mutable._

scala> val map1 = Map("scala" -> 1,"java" -> 2,"python" ->3)
map1: scala.collection.mutable.Map[String,Int] = Map(scala -> 1, java -> 2, python -> 3)
scala> map1("scala") = 6

scala> map1("scala")
res20: Int = 6

scala> map1.getOrElse("c#",-1)
res21: Int = -1

元组

scala> ("scala",1)
res22: (String, Int) = (scala,1)

scala> val t = ( "scala",100L,3.14,("spark",1))
t: (String, Long, Double, (String, Int)) = (scala,100,3.14,(spark,1))

scala> t._1
res23: String = scala

scala> t._3
res24: Double = 3.14

scala> t._4
res25: (String, Int) = (spark,1)
scala> t._4._1
res26: String = spark

scala> val t,(a,b,c,d) = ("scala",100L,3.14,("spark",1))
t: (String, Long, Double, (String, Int)) = (scala,100,3.14,(spark,1))
a: String = scala
b: Long = 100
c: Double = 3.14
d: (String, Int) = (spark,1)

scala> val arr = Array(("lulu",21),("ningning",25),("xiaoyueyue",29))
arr: Array[(String, Int)] = Array((lulu,21), (ningning,25), (xiaoyueyue,29))

scala> arr.toMap
res27: scala.collection.immutable.Map[String,Int] = Map(lulu -> 21, ningning -> 25, xiaoyueyue -> 29)
scala> val arr1 = Array("lulu","ningning","xiaoyueyue")
arr1: Array[String] = Array(lulu, ningning, xiaoyueyue)

scala> val arr2 = Array(24,25,26,27)
arr2: Array[Int] = Array(24, 25, 26, 27)

scala> arr1 zip arr2
res28: Array[(String, Int)] = Array((lulu,24), (ningning,25), (xiaoyueyue,26))

scala> val arr2 = Array(24,25,26,27)
arr2: Array[Int] = Array(24, 25, 26, 27)

scala> arr1.zip(arr2)
res29: Array[(String, Int)] = Array((lulu,24), (ningning,25), (xiaoyueyue,26))

集合

集合之seq

scala> val list = List(1,2,3)
list: List[Int] = List(1, 2, 3)

scala> val list2 = 0 :: list
list2: List[Int] = List(0, 1, 2, 3)

scala> val list3 = list .::(0)
list3: List[Int] = List(0, 1, 2, 3)

scala> val list4 = 0 +: list
list4: List[Int] = List(0, 1, 2, 3)

scala> val list5 = list .+:(0)
list5: List[Int] = List(0, 1, 2, 3)

scala> val list6 = list :+ 4
list6: List[Int] = List(1, 2, 3, 4)

scala> val list7 = List(5,6,7)
list7: List[Int] = List(5, 6, 7)

scala> val list8 = list ++ list7
list8: List[Int] = List(1, 2, 3, 5, 6, 7)
scala> val list8 = list ++: list7
list8: List[Int] = List(1, 2, 3, 5, 6, 7)
scala> val list9 = list ++: list7
list9: List[Int] = List(1, 2, 3, 5, 6, 7)
scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer

scala> val list1 = ListBuffer(1,2,3)
list1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)

scala> list1 += 4
res0: list1.type = ListBuffer(1, 2, 3, 4)

scala> list1
res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4)

scala> list1.append(5)

scala> val list2 = ListBuffer(6,7,8)
list2: scala.collection.mutable.ListBuffer[Int] = ListBuffer(6, 7, 8)

scala> list1 ++= list2
res3: list1.type = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8)

scala> list1 ++ list2
res4: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8, 6, 7, 8)

scala> list1 :+ 4
res5: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8, 4)

集合之set

scala> import scala.collection.immutable.HashSet
import scala.collection.immutable.HashSet

scala> val set1 = new HashSet[Int]()
set1: scala.collection.immutable.HashSet[Int] = Set()

scala> val set2 = set1 + 1
set2: scala.collection.immutable.HashSet[Int] = Set(1)

scala> val set3 = set2 ++ Set(2,3,4)
set3: scala.collection.immutable.HashSet[Int] = Set(1, 2, 3, 4)

scala> import scala.collection.mutable.HashSet
import scala.collection.mutable.HashSet

scala> val set1 = new HashSet[Int]()
set1: scala.collection.mutable.HashSet[Int] = Set()

scala> set1 += 1
res6: set1.type = Set(1)

scala> set1 += 2
res7: set1.type = Set(1, 2)

scala> set1.add(3)
res8: Boolean = true

scala> set1 ++= Set(4,5,6)
res9: set1.type = Set(1, 5, 2, 6, 3, 4)

scala> set1 ++= Set(5,6,7)
res10: set1.type = Set(1, 5, 2, 6, 3, 7, 4)

scala> set1 -= 1
res11: set1.type = Set(5, 2, 6, 3, 7, 4)

scala> set1.remove(2)
res12: Boolean = true

集合之map

scala> val map1 = new HashMap[String,Int]()
<console>:11: error: not found: type HashMap
       val map1 = new HashMap[String,Int]()
                      ^

scala> import scala.collection.mutable._
import scala.collection.mutable._

scala> val map1 = new HashMap[String,Int]()
map1: scala.collection.mutable.HashMap[String,Int] = Map()

scala> map1("scala") = 1

scala> map1 += (("java",2))
res14: map1.type = Map(scala -> 1, java -> 2)

scala> map1 += (("python",3),("c#",4))
res15: map1.type = Map(scala -> 1, java -> 2, c# -> 4, python -> 3)

scala> map1.put("c++",5)
res16: Option[Int] = None

scala> map1.put("c++",5)
res17: Option[Int] = Some(5)

scala> map1
res18: scala.collection.mutable.HashMap[String,Int] = Map(scala -> 1, c++ -> 5, java -> 2, c# -> 4, python -> 3)

scala> map1 -= "java"
res19: map1.type = Map(scala -> 1, c++ -> 5, c# -> 4, python -> 3)

scala> map1.remove("c++")
res20: Option[Int] = Some(5)

scala> map1
res21: scala.collection.mutable.HashMap[String,Int] = Map(scala -> 1, c# -> 4, python -> 3)

相关文章

  • 映射、元组、集合

    映射 元组 集合 集合之seq 集合之set 集合之map

  • Scala菜鸟的进阶之路之映射和元组三

    元组 1、映射是键/值对偶的集合。对偶是元组(tuple)的最简单形态——元组是不同类型的值的集合。 元组的值是通...

  • scala入门(2)

    3.数组、映射、元组、集合 3.1数组 3.1.1定长数组和变长数组 object ArrayDemo {def ...

  • 数据结构

    序列(如列表和元组)、映射(如字典)以及集合(set)是三类主要的数据结构(也称为容器)。 序列(列表、元组和字符...

  • Java & Groovy & Scala & Kotlin -

    Overview 本节主要介绍映射和元组。 映射 (Map) 是由键值对组成的一组数据的集合,在有些语言中也被称为...

  • Scala-元组、集合

    1、元祖映射是K/V对偶的集合,对偶是元组的最简单形式,元组可以装着多个不同类型的值。1)、创建元祖 2)、获取元...

  • Scala-元组、集合

    1、元祖映射是K/V对偶的集合,对偶是元组的最简单形式,元组可以装着多个不同类型的值。1)、创建元祖 2)、获取元...

  • Scala-元组、集合

    1、元祖映射是K/V对偶的集合,对偶是元组的最简单形式,元组可以装着多个不同类型的值。1)、创建元祖 2)、获取元...

  • Scala-元组、集合

    1、元祖映射是K/V对偶的集合,对偶是元组的最简单形式,元组可以装着多个不同类型的值。1)、创建元祖 2)、获取元...

  • Scala-元组、集合

    1、元祖映射是K/V对偶的集合,对偶是元组的最简单形式,元组可以装着多个不同类型的值。1)、创建元祖//创建元祖时...

网友评论

      本文标题:映射、元组、集合

      本文链接:https://www.haomeiwen.com/subject/agzcnxtx.html