40th: Set, Map, TreeSet, treemap operation code Combat

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.