Scala 基础篇之二

一、集合/数组

1、Array

Array在Scala中指的是数组,创建方式如下所示,可以看出分配大小为10,每个元素都分配了默认值。
	scala> var array=new Array[Int](10)
	array: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

修改指定位置元素:
	scala> array(0)=1
	scala> array
	res57: Array[Int] = Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0)

	scala> array.update(1,2)//参数 1下标,2值
	scala> array
	res62: Array[Int] = Array(1, 2, 0, 0, 0, 0, 0, 0, 0, 0)
	
获取数组长度:(length,size)都可以
	scala> array.length
	res63: Int = 10
	
	scala> array.size
	res64: Int = 10
	
数组去重:
	scala> array 
	res66: Array[Int] = Array(1, 2, 0, 0, 0, 0, 0, 0, 0, 0)
	
	scala> array.distinct
	res67: Array[Int] = Array(1, 2, 0)
	

2、Range

 区间类型。本质上就是一种特殊的Array.
 相比较于Array,该Range只读,不允许修改:
		scala> 0 to 5
		res0: scala.collection.immutable.Range.Inclusive = Range(0, 1, 2, 3, 4, 5)
		
		scala> var range= 0 to 10 by 3 (步长)
		range: scala.collection.immutable.Range = Range(0, 3, 6, 9)
	
		scala> var range=0 until 10 by 2
		range: scala.collection.immutable.Range = Range(0, 2, 4, 6, 8)

		scala>  var range= new Range(0,10,2)//从0开始时,10结束 步长为2.
		range: scala.collection.immutable.Range = Range(0, 2, 4, 6, 8)


3、Vector(向量)

Vector 是一个容器,可以保存其他数据对象,也称为集合.
按照下标顺序排列的向量,向量不支持修改。
	声明方式一:
		scala> var vec=Vector(0,10,1)//伴生对象创建。
		vec: scala.collection.immutable.Vector[Int] = Vector(0, 10, 1)
		
	声明方式二:
		scala> var vector=for(i<- 0  to 10 by 2) yield i
		vector: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 2, 4, 6, 8, 10)  //有下标的序列。

4、Iterator(迭代器)

迭代完之后,isEmpty=>true
	scala> var it=Iterator(1,2,3)
	it: Iterator[Int] = non-empty iterator
	
	scala> it.foreach(println)
	1
	2
	3
	
	scala> it.isEmpty
	res41: Boolean = true


5、List (不可修改元素)

List Api使用::/+:   //都是在左追加一个元素
 :::      //集合的合并
 drop/dropRight
 slice 
 revserse	
 take/takeRight
 takeWhile
 head/last/tail
 
注意:list内元素也不允许修改

	(1)、声明一个list:
		scala> var list=List(1,2,3)
		list: List[Int] = List(1, 2, 3)2)、左追加元素  ::/ +: (冒号,+冒号)
		scala> list::=10
		scala> list
		res84: List[Int] = List(10, 1, 2, 3)
	
		scala> list +:= -1
		scala> list
		res87: List[Int] = List(-1, 10, 1, 2, 3)
	
	访问:list
			res0: List[Int] = List(1, 2, 3, 4, 5)
			scala> list(2)
			res1: Int = 33)、集合的合并:::(三冒号)
		scala> var list=List(1,2,3)
		scala> var prefix=List(4,5,6)
		scala> list :::= (prefix) //在list元素之前追加prefix元素,生成新的集合
		scala> list
		res32: List[Int] = List(4, 5, 6, 1, 2, 3)4)、drop/dropRight 删除
			scala> var list=List(1,2,3)
			list: List[Int] = List(1, 2, 3)
			scala> list.drop(2) //删除前n个元素,生成新的 List
			res30: List[Int] = List(3)
			
			scala> list.dropRight(2)//删除后n个元素,生成新的 List
			res9: List[Int] = List(1)
			scala> res9
			res10: List[Int] = List(1)5)、截取 slice (从1开始,3下标结束)
			scala> var list=List(1,2,3)
		    list: List[Int] = List(1, 2, 3)
		
			scala> list.slice(1,3)
			res104: List[Int] = List(2, 3)6)、revserse	反转 
		scala> var list=List(1,2,3)
		list: List[Int] = List(1, 2, 3)
		scala> list.reverse
		res105: List[Int] = List(3, 2, 1)6)、take/takeRight(从左边或 右边取两个元素得到 新的List)
		scala> var list=List(1,2,3)
		list: List[Int] = List(1, 2, 3)
		
		scala> list.take(2)
		res106: List[Int] = List(1, 2)
		scala> list.takeRight(2)
		res107: List[Int] = List(2, 3)7)、takeWhile(获取满足条件的元素)
		scala> var list=List(1,2,3)
		list: List[Int] = List(1, 2, 3)
		
		//获取小于3的元素
		scala> list.takeWhile(x=> x<3 ) 
		res108: List[Int] = List(1, 2)
		
		//获取不能被2整除的元素
		scala> list.takeWhile(x=> x%2==1 )
		res110: List[Int] = List(1)8)、head/last/tail(第一个/最后一个/除了第一个元素的其他元素)
		scala> var list=List(1,2,3)
		list: List[Int] = List(1, 2, 3)
		
		scala> list.head
		res111: Int = 1
		
		scala> list.tail
		res112: List[Int] = List(2, 3)
		
		scala> list.last
		res113: Int = 3
		

6 、ListBuffer(可修改元素)

	 drop/dropRight
	 slice 
	 revserse	
	 take/takeRight
	 takeWhile
	 head/last/tail 
	 
	  +=:  //左添加 元素   ++=: //左添加 List
      +=   //右添加 元素   ++=  //右添加 List
     
      -    	//去除某个元素,形成新的集合。
      -=    //修改原来的List。移除某个元素
       :::      //集合的合并

	  remove(下标) //删除指定位置元素 修改List自身
	  insert    //在指定位置之前插入元素
	  insertAll //在指定位置之前插入List
	  prependToList	 //将list1结果添加到bufferlist2中
	  
scala> import scala.collection.mutable._
	import scala.collection.mutable._
	
	scala> var list=ListBuffer[Int]()
	list: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
	scala> var list=ListBuffer(1,2,3)
	list: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
	scala> list.update(0,10)
	scala> list
	res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(10, 2, 3)

------------------------------------------------------------------------
		scala>  var list=ListBuffer(1,2,3)
		list: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
		
		scala> list.+=(4)//右追加元素
		res12: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4)
		
		scala> list.+=:(5)//左追加元素
		res13: scala.collection.mutable.ListBuffer[Int] = ListBuffer(5, 1, 2, 3, 4)
		
		scala> list.++=(List(6,7,8)) //右追加 List
		res14: scala.collection.mutable.ListBuffer[Int] = ListBuffer(5, 1, 2, 3, 4, 6, 7, 8)
		scala> list.++=(List(6,7,8)) //左追加 List
			res14: scala.collection.mutable.ListBuffer[Int] = ListBuffer(,6, 7, 85, 1, 2, 3, 4)

----------------------------------------------------------------------
	scala> var list=ListBuffer(1,2,3,1)
	list: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 1)
	
	//去除某个元素,形成新的集合。
	scala> list.-(1) 
	res18: scala.collection.mutable.ListBuffer[Int] = ListBuffer(2, 3, 1)
	scala> list
	res19: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 1)
	
	//修改原来的List。
	scala> list.-=(1) 
	res20: scala.collection.mutable.ListBuffer[Int] = ListBuffer(2, 3, 1)
	scala> list
	res21: scala.collection.mutable.ListBuffer[Int] = ListBuffer(2, 3, 1)
	
--remove(下标) //删除指定位置元素 修改List自身----------------
	scala> var list=ListBuffer(1,2,3)
	list: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
	scala> list.remove(0)
	res25: Int = 1
	scala> list
	res26: scala.collection.mutable.ListBuffer[Int] = ListBuffer(2, 3)

--insert //在指定位置之前插入元素 insertAll //在指定位置之前插入List-------
	  
	 
	scala> list
	res27: scala.collection.mutable.ListBuffer[Int] = ListBuffer(2, 3)
	
	scala> list.insert(0,4)
	
	scala> list
	res29: scala.collection.mutable.ListBuffer[Int] = ListBuffer(4, 2, 3)
	
	scala> list.insertAll(2,List(1,2,3))
	scala> list
	res31: scala.collection.mutable.ListBuffer[Int] = ListBuffer(4, 2, 1, 2, 3, 3)

------//将list1结果添加到list2--------------------------------
	scala>  var list1=List(1,2,3)
	list1: List[Int] = List(1, 2, 3)
	
	scala>  var list2=ListBuffer(0,0)
	list2: scala.collection.mutable.ListBuffer[Int] = ListBuffer(0, 0)
	
	scala> list2.prependToList(list1)
	res34: List[Int] = List(0, 0, 1, 2, 3)
	
	scala> list2
	res36: scala.collection.mutable.ListBuffer[Int] = ListBuffer(0, 0, 1, 2, 3)

二、set 的使用

2.1、Set(不可修改)

	+  	//创建新的Set并添加元素
	+=  // 修改原始set
	-  	创建新的Set并去除某元素
	-=  // 修改原始set
	size
	
(1)、新增元素( += 修改原始set)
	scala> var s=Set[Int](1,2,3)
	s: scala.collection.immutable.Set[Int] = Set(1, 2,3)
	scala> s.+=(4)
	scala> s
	s: scala.collection.immutable.Set[Int] = Set(1, 2, 3,4)

	scala> s+(7) //创建新的Set并添加元素
	res12: scala.collection.immutable.Set[Int] = Set(1,2,3,4,7)2)去除元素
	scala> var s=Set[Int](1,2,4,6,2)
		s: scala.collection.immutable.Set[Int] = Set(1, 2, 4, 6)
		scala> s.-=(1)
		scala> s
		s: scala.collection.immutable.Set[Int] = Set( 2, 4, 6)
	
		scala> s+(2) //创建新的Set并添加元素
		res12: scala.collection.immutable.Set[Int] = Set(4, 6)

(3) 获取set长度
	scala> var s=Set[Int](1,2,4,6,2)
	s: scala.collection.immutable.Set[Int] = Set(1, 2, 4, 6)
	
	scala> s.size
	res23: Int = 4		
	

2.2、Set(可修改)

	+  	//创建新的Set并添加元素
	+=  // 修改原始set
	-  	创建新的Set并去除某元素
	-=  // 修改原始set
	remove/add
----------------------------------------------------------------------
		scala> var s=scala.collection.mutable.Set(1,2,3)
		s: scala.collection.mutable.Set[Int] = Set(1, 2, 3)
		scala> s.+(4)
		res25: scala.collection.mutable.Set[Int] = Set(1, 2, 3, 4)
		scala> s
		res26: scala.collection.mutable.Set[Int] = Set(1, 2, 3)
		
		scala> s.+=(4)
		res27: scala.collection.mutable.Set[Int] = Set(1, 2, 3, 4)
		scala> s
		res28: scala.collection.mutable.Set[Int] = Set(1, 2, 3, 4)
		
		scala> s.-(2)
		res29: scala.collection.mutable.Set[Int] = Set(1, 3, 4)
		scala> s
		res30: scala.collection.mutable.Set[Int] = Set(1, 2, 3, 4)
		
		scala> s.-=(2)
		res31: scala.collection.mutable.Set[Int] = Set(1, 3, 4)
		scala> s
		res32: scala.collection.mutable.Set[Int] = Set(1, 3, 4)
----------------------------------------------------------------------
		scala> var s=scala.collection.mutable.Set(1,2,3)
		s: scala.collection.mutable.Set[Int] = Set(1, 2, 3)
		
		scala>  s.remove(2)
		res37: Boolean = true
		scala>  s.remove(-1)
		res38: Boolean = false
		
		scala> s
		res39: scala.collection.mutable.Set[Int] = Set(1, 3)
		scala> s.add(4)
		res41: Boolean = true

三、 HashMap

3.1 HashMap(不可变)

	+   -  		//形成新的集合 
	+=  -=  	//修改自身集合
	get(key)  //通过key 获取值
	size        //获取map长度
	遍历
	merged      //map的的合并
	keys/values   //直接点
-------------------------------------------------------
(1)+/-  +=/-=
	scala> import scala.collection.immutable.HashMap
	import scala.collection.immutable.HashMap
	scala> var hm=HashMap[String,String](("建设","001"),("招商","002"))
	hm: scala.collection.immutable.HashMap[String,String] = Map(建设 -> 001, 招商 -> 002)
	scala> hm+="工商"->"003"
	scala> hm.+=(("工商1","004"))
	scala> hm
	res4: scala.collection.immutable.HashMap[String,String] = Map(工商 -> 003, 建设 -> 001, 招商 -> 002, 工商1 -> 004)
	
	scala> hm.-("工商")
	res6: scala.collection.immutable.HashMap[String,String] = Map(建设 -> 001, 招商 -> 002, 工商1 -> 004)
	scala> hm
	res7: scala.collection.immutable.HashMap[String,String] = Map(工商 -> 003, 建设 -> 001, 招商 -> 002, 工商1 -> 004)
	
	scala> hm.-=("工商")
	scala> hm
	res9: scala.collection.immutable.HashMap[String,String] = Map(建设 -> 001, 招商 -> 002, 工商1 -> 004)
-------------------------------------------------------
(2)、get:
		scala> import scala.collection.immutable.HashMap
		import scala.collection.immutable.HashMap
		scala> var hm=HashMap[String,String](("建设","001"),("招商","002"))
		hm: scala.collection.immutable.HashMap[String,String] = Map(建设 -> 001, 招商 -> 002)
		
		//没有获取到,则为默认值。
		scala> hm.get("招商").getOrElse("没有值") 
		res11: String = 002
		scala> hm.get("招商1").getOrElse("没有值")
		res12: String = 没有值

//map的遍历	
		scala> for(i<- hm) println(i._1+" -> "+i._2)
			工商 -> 003
			建设 -> 001
			招商 -> 002
			工商1 -> 004	

---------------------------------------
(3)、merged:
	scala> var hm=HashMap[String,String](("建设","001"),("招商","002"))
	hm: scala.collection.immutable.HashMap[String,String] = Map(建设 -> 001, 招商 -> 002)
	scala> var newhm=HashMap[String,String](("邮政","004"))
	newhm: scala.collection.immutable.HashMap[String,String] = Map(邮政 -> 004)                   

scala> var hm=HashMap[String,String](("建设","001"),("招商","002"))
hm: scala.collection.immutable.HashMap[String,String] = Map(建设 -> 001, 招商 -> 002)

scala> var newhm=HashMap[String,String](("邮政","004"))
newhm: scala.collection.immutable.HashMap[String,String] = Map(邮政 -> 004)                                                               
//定义合并规则                                                           
scala> var mergerFunction:((String,String),(String,String))=>(String,String) = (tuple1,tuple2)=>{
     |    if(tuple1._1.equals(tuple2._1)){
     |       (tuple1._1,tuple2._2)
     |    }else{
     |      tuple1
     |    }
     |  }

scala> var res=hm.merged(newhm)(mergerFunction)
res: scala.collection.immutable.HashMap[String,String] = Map(邮政 -> 004, 建设 -> 001, 招商 -> 002)

scala> res
res18: scala.collection.immutable.HashMap[String,String] = Map(邮政 -> 004, 建设 -> 001, 招商 -> 002)

scala> for(i<- res) println(i)
(邮政,004)
(建设,001)
(招商,002)

-------------------------------
(4)、keys/values
	scala> var hm=HashMap[String,String](("建设","001"),("招商","002"))
	hm: scala.collection.immutable.HashMap[String,String] = Map(建设 -> 001, 招商 -> 002)
	
	scala> hm.keys
	res23: Iterable[String] = Set(建设, 招商)
	scala> hm.values
	res24: Iterable[String] = MapLike(001, 002)
	

3.2 HashMap(可变)

remove/put
	 scala> import scala.collection.mutable.HashMap
	import scala.collection.mutable.HashMap
	
	scala> var hm=HashMap[String,String](("建设","001"),("招商","002"))
	hm: scala.collection.mutable.HashMap[String,String] = Map(建设 -> 001, 招商 -> 002
	                                                          
	scala> hm+="工商"->"003"
	res0: scala.collection.mutable.HashMap[String,String] = Map(工商 -> 003, 建设 -> 001, 招商 -> 002)
	
	scala> hm-=("工商")
	res1: scala.collection.mutable.HashMap[String,String] = Map(建设 -> 001, 招商 -> 002)
	
	scala> hm.put("邮政","004")
	res2: Option[String] = None
	 
	scala> hm.remove("建设")
	res7: Option[String] = Some(001)
	 
	scala> for(i<- hm) println(i)
	(邮政,004)
	(招商,002)
	 

四、集合算子

4.1 排序分组

4.1.1 sorted(Int)

Array:
def sorted[B >: Int](implicit ord: scala.math.Ordering[B]): Array[Int]

	scala> var array=Array(1,2,4,5,3)
	array: Array[Int] = Array(1, 2, 4, 5, 3)
	scala> array.sorted
	res17: Array[Int] = Array(1, 2, 3, 4, 5) //新的Array

List:
	scala> List(1,2,5,4,3)
	res18: List[Int] = List(1, 2, 5, 4, 3)
	scala> res18
	scala> List(1,2,5,4,3)
	res18: List[Int] = List(1, 2, 5, 4, 3)
	scala> res18.sorted
	res20: List[Int] = List(1, 2, 3, 4, 5) //新的List
	scala> res18
	res21: List[Int] = List(1, 2, 5, 4, 3)

4…1.2 sortBy(元组)

def sortBy[B](f: (String,Int) => B)(implicit ord: scala.math.Ordering[B]): List[(String,Int)]

scala> var list=Array(("a",1),("b",2),("d",5),("c",4))
list: Array[(String, Int)] = Array((a,1), (b,2), (d,5), (c,4))

scala> list.sortBy(t=>t._2) // list.sortBy(_._2) 第一个_等价于入参占位。
res7: Array[(String, Int)] = Array((a,1), (b,2), (c,4), (d,5))

scala> list.sortBy(t=>t._1) //list.sortBy(_._2)
res8: Array[(String, Int)] = Array((a,1), (b,2), (c,4), (d,5))

4.1.3 sortWith

用户可以指定多个Field做联合排序

	def sortWith(lt: ((String, Int), (String, Int)) => Boolean): Array[(String, Int)]
	
	scala> var list=Array(("a",2),("a",1),("b",2),("d",5),("c",4))
	list: Array[(String, Int)] = Array((a,2), (a,1), (b,2), (d,5), (c,4))
	
	scala>     val tuples = list.sortWith((t1, t2) => {
	     |       if (!t1._1.equals(t2._1)) {
	     |         t1._1.compareTo(t2._1) < 0 
	     |       } else {
	     |         t1._2.compareTo(t2._2) < 0
	     |       }
	     |     })
	tuples: Array[(String, Int)] = Array((a,1), (a,2), (b,2), (c,4), (d,5))

4.1.4 flatten(拉平)

数组中还是数组 flatten:
	scala> var list=Array(Array(1,2,3),Array(4,5))
	scala> list.flatten
	res6: Array[Int] = Array(1, 2, 3, 4, 5)

4.1.5 map(转换)

	scala> var list=Array("this is a demo","hello word")
	list: Array[String] = Array(this is a demo, hello word)
	
	scala> list.map(line=>line.split(" ")) // 等价list.map(_.split(" "))
	res13: Array[Array[String]] = Array(Array(this, is, a, demo), Array(hello, word))
	
小结:
	scala> list.map(_.split(" ")).flatten.map((_,1))
	res14: Array[(String, Int)] = Array((this,1), (is,1), (a,1), (demo,1), (hello,1), (word,1))

4.1.6 flatMap

将array 转化为新的 array:
	scala>  var list=Array("this is a demo","hello word")
	list: Array[String] = Array(this is a demo, hello word)
	
	scala> list.flatMap(_.split(" "))
	res16: Array[String] = Array(this, is, a, demo, hello, word)
	
	scala> list.flatMap(_.split(" ")).map((_,1))
	res17: Array[(String, Int)] = Array((this,1), (is,1), (a,1), (demo,1), (hello,1)
	, (word,1))
	

4.1.7 filter/filterNot

//过滤,包含某元素/不包含
	scala>  var list=Array("this is a demo","hello word")
	list: Array[String] = Array(this is a demo, hello word)
	
	scala> list.filter(line=>line.contains("hello"))
	res24: Array[String] = Array(hello word)
	
	scala> list.filterNot(line=>line.contains("hello"))
	res25: Array[String] = Array(this is a demo)

4.1.8 groupBy

分组:
	scala> var list=Array("good","good","study")
	list: Array[String] = Array(good, good, study)
	
	scala> list.groupBy(item=>item)
	res30: scala.collection.immutable.Map[String,Array[String]] = 
		Map(study -> Array(study), good -> Array(good, good))
	
	scala> list.groupBy(item=>item).map(t=>(t._1,t._2.size))
	res34: scala.collection.immutable.Map[String,Int] = Map(study -> 1, good -> 2)
	
	scala> res34.toList //map转List 
	res8: List[(String, Int)] = List((study,1), (good,2))

	scala> res8.sortBy(_._2)
	res10: List[(String, Int)] = List((study,1), (good,2))

4.2 聚合

4.2.1 fold

	scala> var lst=Array(1, 2, 3, 1, 2, 4, 5, 6, 7, 9, 4)
	lst: Array[Int] = Array(1, 2, 3, 1, 2, 4, 5, 6, 7, 9, 4)
	
	scala> lst.fold[Int](0)((v1,v2)=>v1+v2) // lst.fold(0)(_+_)
	res52: Int = 44
	计算逻辑:
			v1=0+1
			v1=v1+2
			v1=v1+3
			...

4.2.2 aggregate

在这里插入图片描述

	scala> var lst=Array(1, 2, 3, 1, 2, 4, 5, 6, 7, 9, 4)
	lst: Array[Int] = Array(1, 2, 3, 1, 2, 4, 5, 6, 7, 9, 4)
	
	scala> lst.aggregate
	def aggregate[B](z: => B)(seqop: (B, Int) => B,combop: (B, B) => B): B
	
	scala> lst.aggregate(0)((v1,v2)=>v1+v2,(b1,b2)=>b1+b2) //lst.aggregate(0)(_+_,_+_)
	res53: Int = 44


4.2.3 reduce


scala> var lst=Array(1, 2, 3, 1, 2, 4, 5, 6, 7, 9, 4)
lst: Array[Int] = Array(1, 2, 3, 1, 2, 4, 5, 6, 7, 9, 4)

scala> lst.reduce
   def reduce[A1 >: Int](op: (A1, A1) => A1): A1

scala> lst.reduce((v1,v2)=>v1+v2)//lst.reduce(_+_)
res54: Int = 44

4.2.4 zip

scala> var v=Vector(1,2,4)
v: scala.collection.immutable.Vector[Int] = Vector(1, 2, 4)

scala> v.zip(Array("a","b","c"))
res52: scala.collection.immutable.Vector[(Int, String)] = Vector((1,a), (2,b), (4,c))

4.2.5 unzip

	scala> var v=List(("a",1),("b",2),("c",3))
	v: List[(String, Int)] = List((a,1), (b,2), (c,3))
	
	scala> v.unzip
	res53: (List[String], List[Int]) = (List(a, b, c),List(1, 2, 3))

4.3 其他

4.3.1 字符统计

	var arrs=Array("this is a demo","good good study","day day up")
	arrs.flatMap(_.split(" "))
	.groupBy(word=>word)
	.toList
	.map(tuple=>(tuple._1,tuple._2.size))
	.sortBy(_._1)
	.foreach(println)
	

4.3.2 元组计算

	var arrs=Array("this is a demo","good good study","day day up")
	arrs.flatMap(_.split(" "))
	.map((_, 1))
	.groupBy(_._1)
	.toList
	.map(t=>(t._1,t._2.map(_._2).sum))
	.sortBy(_._2)
	.foreach(println)
	

4.3.4 diff|intersect|union

	scala> var v=List(1,2,3)
	v: List[Int] = List(1, 2, 3)
	scala> v.diff(List(2,3,5))//差集
	res54: List[Int] = List(1)
	
	scala> var v=List(1,2,3,5)
	v: List[Int] = List(1, 2, 3, 5)
	scala> v.intersect(List(2,4,6))
	res55: List[Int] = List(2) //交集
	
	scala> var v=List(1,2,3,5)
	v: List[Int] = List(1, 2, 3, 5)
	scala> v.union(List(2,4,6))  //并集
	res56: List[Int] = List(1, 2, 3, 5, 2, 4, 6)

4.3.5 distinct 去重

	scala> var v=List(1,2,3,3,5)
	v: List[Int] = List(1, 2, 3, 3, 5)
	scala> v.distinct
	res57: List[Int] = List(1, 2, 3, 5)
	
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值