[scala] scala 集合(⑧)

1.List 基础操作

2.Set 基础操作

3. TreeSet 排序Set

4. 拉链操作

5. 流

 

import scala.collection.immutable.TreeSet
import scala.util.Random

/**
  * @author xwolf
  * @date 2017-04-25 15:26
  * @since 1.8
  */
object Collection {

  def main(args:Array[String]):Unit={

     list()
     // set()
    //treeSet()
  }

  //Seq
  def seq():Unit={
    val seq = Seq(32,3,43,3,2,321,4)
    println(seq)
  }

  //List
  def list():Unit={
    var list = List(3,24,123,7,4)
    //头部追加元素
    list=12::list
    list=list++Set(101,5)
    list = list.+:(14)
    val lst =List(54,65)
    //list 追加
    list = lst.:::(list)
    //删除第一个元素
    list=list.drop(1)
    // 和下面写法等价
    // list=lst:::list
    println(list)
    //排序
    list=list.sorted
    println(list)
    //自定义排序
    list = list.sortWith((x:Int,y:Int)=> x>y)
    println(list)
    //求和
    val sum = list.sum
    println(sum)

    // 去重并 取出来偶数
    val newList = list.distinct.filter((x:Int)=>x%2==0)
    println(newList)
    val isContains = newList.contains(4)
    println(isContains)
    //遍历输出
    //newList.foreach(println _)

    val secList = List(3,4,35,5)
    //模式匹配求和
    val sums = listSumMatch(secList)
    val sumsec = secList.sum
    println(sums+","+sumsec)

    val a = List("土豆","白菜","猪肉","牛肉")
    val b = List(21,2,43,54)
    val c = zip(a,b)
    println(c)
    val price= c.map(p=>p._2).sum
    println(price)
    stream()
  }
  //Set
  def set():Unit={
    var set =Set(5,2,10,6,3)
    var st =Set(2,31,6)
    //求两个集合的交集
    val eqset = set & st
    println(eqset)
    //追加元素
    set = set++:st
    println(set)
    //删除元素
    set = set.-(2)
    println(set)
    //删除第一个元素
    set = set.drop(1)
    println(set)
    //任意N个元素组成新的set 集合;如果N大于set的长度返回空
   // set.subsets(3).foreach(println _)

    //返回区间之内的元素
    val sliceSet = set.slice(2,5)
    println(sliceSet)

    //是否包含指定元素
    val isContains = sliceSet.contains(3)
    println(isContains)


  }
  //treeSet 排序集合(不可变)
  def treeSet():Unit={
   var ts = TreeSet(32,3,2,5,212,53,5)
    //添加元素
    ts= ts.+(15)
    println(ts)
    ts = ts.-(3)
    println(ts)

    //构造新的集合
    val tree = TreeSet(5,2,3)
    val newTree= tree.+(4)
    println(newTree)

  }

  //list 求和
  def listSum(list: List[Int]):Int={
    if (list == Nil) 0
      //tail 返回除第一个元素外的所有的集合
    else list.head + listSum(list.tail)
  }
  //模式匹配求和
  def listSumMatch(list: List[Int]):Int= list match {
    case Nil => 0
    case h::t => h + listSumMatch(t)
  }

  //拉链操作
  def zip(a:List[String],b:List[Int]):List[(String,Int)] = a zip(b)

  //流式操作
  def stream():Unit={
    val randomList = (1 to 50).map(_ => Random.nextInt(100)).toList
    //val isDivide = randomList.filter(divide(_)).take(3)
   // println(isDivide)

   val nList= randomList.toStream.filter(divide(_)).take(3).toList
    println(nList)

  }
  def divide(n:Int):Boolean={
    val is = n%4==0
    println(s"${is}")
    is
  }

}

 

更多集合类操作:

http://docs.scala-lang.org/zh-cn/overviews/collections/sets.html  

具体的不可变集实体类

posted @ 2017-04-27 17:39  snow__wolf  阅读(222)  评论(0编辑  收藏  举报