scala集合详述

scala数组、5集合

字符串

显示s串中第一个出现b的下标索引

println(s.indexOf(98))

使用 concat() 方法来连接两个字符串

s1.concat(s2)			类似于    s1 + s2

创建一个可以修改的字符串

val buf = new StringBuilder;

集合

​ 集合都可分为可变与不可变

Array

    /**
      * 数组-不可变
      */
     //数组合并
//      val arr = Array[String]("a","c","b","d")
//      val arr1 = Array[String]("ooo","xxx")
//      val arrays : Array[String] = Array.concat(arr,arr1)
//      arrays.foreach(println)

     //数组初始化
//        val strings: Array[String] = Array.fill(3)("hello")
//        strings.foreach(println)

    //一维数组,不可变
//        val aaa = Array[String]("a","c","b","d")
//          val aaa1 =new  Array[Int](3)
//            aaa1(0)=1
//            aaa1(1)=3
//            aaa1(2)=5
//        aaa1.foreach(println)
//        for (elem<-aaa){
//          println(elem)
//        }

    //二维数组
//    val aaa = Array[String]("a","c","b","d")
//    val array = new Array[Array[Int]](3)
//    array(0) = Array[Int](1,2,3)
//    array(1) = Array[Int](1,1,1)
//    array(2) = Array[Int](1,2,3)

    //遍历方法1
//    for (aaa<-array){
//      for (elem<-aaa){
//        println(elem)
//      }
//    }
    //遍历方法2
//    for(aaa<-array;elem<-aaa){
//      println(elem)
//    }
    //遍历方法3
//    array.foreach(aaa=>{aaa.foreach(println)})
/**
  * 数组-可变
  * 需要导包  import scala.collection.mutable.ArrayBuffer
  */
import scala.collection.mutable.ArrayBuffer

val arr = ArrayBuffer[Int](1,2,3)
//末尾添加 +=
arr.+=(4)
//开头添加 +=:
arr.+=:(0)
//追加多个 append
arr.append(6,6,6)
arr.foreach(println)

List

    /**
      * 集合-不可变
      */
    //map是将list集合变为两个数据,flatmap是将集合变为多个数据
//    val list = List[String]("hello scala","hello java","hello spark")
    //    val result: List[String] = list.flatMap(s =>{s.split(" ")})
        //    result.foreach(println)

    //过滤匹配集合中的数据  filter,匹配上的留下
//    val result1: List[String] = list.filter(s => {
//      "hello scala".equals(s)
//    })
//    result1.foreach(println)

    //过滤长度  count
//    val i: Int = list.count(s => {
//      s.length > 2
//    })
//    println(i)

//    val result : List[Array[String]] = list.map(s=>{
//      s.split(" ")
//    })
//    result.foreach(arr=>{arr.foreach(println)})

    //集合的创建和遍历
    val list = List[Int](1,2,3)
    for (elem<-list){
      println(elem)
    }
    list.foreach(println)
/**
  * 集合-可变
  * 导包,方法雷同于array可变操作 append += +=:
  */
  val list1 = ListBuffer[Int](1,2,3)
list1.append(4,4,4)
list1.foreach(println)

Set

import scala.collection.mutable
import scala.collection.immutable

/**
      * Set不可变 去重功能  无序
      * 一些方法 filter。。。
      */
//    val set = Set[Int](1,2,3,4)
//    val set1 = Set[Int](3,4,5,6)

    //集合交集
//    val result: Set[Int] = set.intersect(set1)
//    val result = set & set1

    //集合差集
//    val result: Set[Int] = set.diff(set1)

//    result.foreach(println)

    //遍历
//    for (elem<-set){
//      println(elem)
//    }

//    set.foreach(println)

/**
  * Set-可变
  *手动导包 import scala.collection.mutable.Set +=
  */
  //    val set = Set[Int](1,2,3,4)
//    set.+=(100)
//    set.foreach(println)
//如果可变与不可变交替使用,需要加immutable/mutable关键字
  val set = mutable.Set[Int](1,2,3,4)
  set.+=(100)
  val set2 = immutable.Set[String]("a","b")

Map

 /**
      * Map-不可变
      */
//    val map = Map[String,Int]("a"->100,"b"->200,("c",300),("c",400))
//
//    //map合并
//    val map2 = Map[String,Int](("a",1),("b",2),("c",3))
//    val map3 = Map[String,Int](("a",100),("e",300))
//    //++将后map丢前map中,简而言之,后map重复数据替换前map数据
//    val stringToInt: Map[String, Int] = map2.++(map3)
//    //++:将前map丢后map中
//    val stringToInt1: Map[String, Int] = map2.++:(map3)
//    stringToInt.foreach(print)
//    stringToInt1.foreach(print)
//    val option: Option[Int] = map.get("a")
//    println(option)

    //打印值,如果集合中存在就返回值,不存在就返回("没有值")
//    val option1 = map.get("bbb").getOrElse("没有值")
//    println(option1)

      //根据K的值遍历V
//    val keys: Iterable[String] = map.keys
//    keys.foreach(key=>{
//      val value = map.get(key).get
//      println(s"key=$key, value=$value")
//    })
//    println(keys)
//    val map = Map[String,Int]("a"->100,"b"->200,("c",300),("c",400))
//    println(map)
//    map.foreach(println)
//    for(elem<-map){
//      println(elem)
//    }

/**
  * Map-可变
  * 导包  import scala.collection.mutable.Map
  * count,filter....
  */
import scala.collection.mutable.Map
val map = Map[String,Int]()
map.put("a",1)
map.put("b",2)
//    map.foreach(println)
  //过滤数据,_1,_2是元组中拿数据
    val stringToInt: mutable.Map[String, Int] = map.filter(tp => {
      val key = tp._1
      val value = tp._2
      value == 2
    })
stringToInt.foreach(println)

Tuple

元组中可以存放任意类型,但是最多存放22个元素

元组创建可new,可不mew,也可以直接写出(元素1,元素2,。。。)

val typle1: Tuple1[String] = new Tuple1("hello")
val tuple2: (String, Int) = new Tuple2("a",100)
val tuple3: (Int, Boolean, Char) = new Tuple3(1,true,'C')
val tuple4: (Int, Int, Int, String, Boolean) = (1,3,4,"abc",false)
val tuple22: (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int) = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,1,20,21,22)

val value: Int = tuple22._9

//遍历  productIterator
val iterator: Iterator[Any] = tuple4.productIterator
iterator.foreach(println)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值