码迷,mamicode.com
首页 > 其他好文 > 详细

Scala Set的一些用法

时间:2015-01-29 14:46:49      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

scala> val set = Set(3,2,4,8,1)
set: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 8, 4)

scala> set.min
res58: Int = 1

scala> set.max
res59: Int = 8

scala> set.size
res60: Int = 5

scala> val set1 = Set(9,8)
set1: scala.collection.immutable.Set[Int] = Set(9, 8)

scala> set & set1
res63: scala.collection.immutable.Set[Int] = Set(8)

scala> set.intersect(set1)
res65: scala.collection.immutable.Set[Int] = Set(8)

scala> set ++ set1
res66: scala.collection.immutable.Set[Int] = Set(1, 9, 2, 3, 8, 4)

scala> set -- set1
res67: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)

scala> set &~ set1
res68: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)

scala> set1.count(_ > 5)
res70: Int = 2

scala> set.diff(set1)
res71: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)

scala> set1.diff(set)
res72: scala.collection.immutable.Set[Int] = Set(9)

scala> set.slice(1,4)
res83: scala.collection.immutable.Set[Int] = Set(2, 3, 8)

scala> set.subsets(2).foreach(x=>println(x))
Set(1, 2)
Set(1, 3)
Set(1, 8)
Set(1, 4)
Set(2, 3)
Set(2, 8)
Set(2, 4)
Set(3, 8)
Set(3, 4)
Set(8, 4)



Scala Set的一些用法

标签:

原文地址:http://my.oschina.net/brucegao/blog/373167

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!