惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - cindy_zl

SAP SD销售与分销模块 Agent认知框架ReAct&Plan RAG系统优化 RAG检索优化 RAG架构 SAA-C02-架构师助理级-英文版-Q1 scala-数组 navicat mysql创建存储过程报错 安装完kali出现中文乱码 Spark SQL是处理结构化的数据 SPARK SQL ERROR: Detected cartesian product for INNER join between logical plans报错解决方法 scala的lazy关键字 scala集合三大类(seq序列,set集,map映射)——map映射 scala集合三大类(seq序列,set集,map映射)——list序列 scala元组及拉链操作 scala的map映射 scala数组 scala方法和涵数的声明以及方法转换成涵数 scala的基础数据类型&if条件表达式&for循环
scala集合三大类(seq序列,set集,map映射)——set集合
cindy_zl · 2019-09-18 · via 博客园 - cindy_zl

scala集合三大类(seq序列,set集,map映射)——set集合

set集合

scala> import scala.collection.immutable.HashSet    //导入不可变HashSet包

import scala.collection.immutable.HashSet

scala> val set1 = new HashSet[Int]()                      //new一个HashSet,且范型为Int类型

set1: scala.collection.immutable.HashSet[Int] = HashSet()

scala> val set2 = set1 + 1                            //添加一个元素1,

set2: scala.collection.immutable.HashSet[Int] = HashSet(1)  //生成一个新的set集合,

scala> set1                                                     //set1依然是空的

res66: scala.collection.immutable.HashSet[Int] = HashSet()

scala> set2

res67: scala.collection.immutable.HashSet[Int] = HashSet(1)  //set2是新的集合

scala> val set3 = set2 ++ Set(2,3,4)                                     //使用++操作符可以将集合追加

set3: scala.collection.immutable.HashSet[Int] = HashSet(1, 2, 3, 4)

scala> set3

res68: scala.collection.immutable.HashSet[Int] = HashSet(1, 2, 3, 4)

不可变set: 

scala> import scala.collection.mutable.HashSet

import scala.collection.mutable.HashSet

scala> val set1 = new HashSet[Int]()

set1: scala.collection.mutable.HashSet[Int] = HashSet()

scala> set1 +=1       //添加元素1

res69: set1.type = HashSet(1)

scala> set1

res70: scala.collection.mutable.HashSet[Int] = HashSet(1).   //set1里面添加1成功

scala> set1 += 2        //添加2到set1里

res71: set1.type = HashSet(1, 2)

scala> set1                      

res72: scala.collection.mutable.HashSet[Int] = HashSet(1, 2)  //set1里面添加2成功

scala> set1.add(3)                   //使用 add方法也可以向set集合里面添加元素

res73: Boolean = true

scala> set1

res74: scala.collection.mutable.HashSet[Int] = HashSet(1, 2, 3)

scala> set1 ++= Set(4,5,6)                         //向set1里面添加一个set里面的内容追加,使用 ++= 操作方法

res75: set1.type = HashSet(1, 2, 3, 4, 5, 6)

scala> set1

res76: scala.collection.mutable.HashSet[Int] = HashSet(1, 2, 3, 4, 5, 6)    //追加一个set内容成功

scala> set1 ++= Set(5,6,7)        //再追加一个set集合元素内容

res77: set1.type = HashSet(1, 2, 3, 4, 5, 6, 7)

scala> set1

res78: scala.collection.mutable.HashSet[Int] = HashSet(1, 2, 3, 4, 5, 6, 7)  //集合内容发现set是具有去重的操作

scala> set1 -= 1        //使用 -= 操作方法可以移除一个元素

res79: set1.type = HashSet(2, 3, 4, 5, 6, 7)

scala> set1.remove(2)              //也可以使用remove方法移除元素

res80: Boolean = true

scala> set1

res81: scala.collection.mutable.HashSet[Int] = HashSet(3, 4, 5, 6, 7)