Let's take a look at the operation of set map today, so we start with the code
Val data = mutable. Set.empty[int]
Data ++= list (+/-)//Add to table on empty set
Data + = 4//Add new element to set
Data--= List (2,3)
println (data)//set (1, 4)
Data + = 1
println (data)//set (1, 4), stating that the Set element cannot be duplicated
Data.clear ()
println (data)//set ()
We can see that the set and list operations are roughly the same, but the important difference is that there are no duplicate elements in the set!
Let's take a look at map operations.
Val map = mutable. Map.empty[string,string]
Map ("java") = "Hadoop"
Map ("Scala") = "Spark"
println (map)//map (Scala, Spark, Java, Hadoop)
println (Map ("Scala"))//spark
Map stores data in the form of key value, and all keys, all of which are of the same type.
In fact, the set and map when adding elements, there is no order, but we actually apply, may need to have a sequence of set or map, then, the use of TreeSet and TreeMap
Val treeset = TreeSet (9,3,1,8,0,2,7,4,6,5)
println (treeset)//treeset (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Val Treesetforchar = TreeSet ("Spark", "Scala", "Hadoop")
println (Treesetforchar)//treeset (Hadoop, Scala, Spark)
var treemap=treemap ("Scala", "Spark", "Java", "Hadoop")
println (TREEMAP)//map (Java, Hadoop, Scala---Spark)
We can see that the printed results are well sequenced.
Share more of the Scala resources:
Baidu Cloud Disk: http://pan.baidu.com/s/1gd7133t
Micro Cloud Disk: http://share.weiyun.com/047efd6cc76d6c0cb21605cfaa88c416
360 Cloud Disk: Http://yunpan.cn/cQN9gvcKXe26M (extract code: 13CD)
Information from DT Big Data Dream Factory public account: Dt_spark
Follow the account for more information about Scala learning
40th: Set, Map, TreeSet, treemap operation code Combat