Lesson_collections

package com.bjsxt.scala

object Lesson_collections {
  def main(args: Array[String]): Unit = {
    /**
     * 元组
     * 	1.元组最多支持22元素
     *  2.声明元组 可以new 可以不new 也可以直接()
     *  3.遍历:tuple.productIterator
     * 
     */
    val tuple1 = new Tuple1(1)
    val tuple2 = new Tuple2(1,2)
    val tuple3 = new Tuple3(1,2,3)
    val tuple4 = Tuple4(1,'a',3,"abc")
//    println(tuple4._4)
    val tuple5 = (1,2,3,4,5,5)
    val tuple22 = new Tuple22(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,"hello",22)
//    println(tuple22._21)
    
    val iter = tuple22.productIterator
    while(iter.hasNext){
      val one = iter.next()
      println(one)
    }
//    iter.foreach(println)
    
    /**
     * map
     * map中的元素是一个个的tuple2 二元组
     */
//    val map = Map(1->"a",2->"b",(3,"c"),(3,"ccc"))
    //遍历所有values
//    val values = map.values
//    for(value <-values){
//      println(value)
//    }
//    
//    val result = map.get(300).getOrElse("1000")
//    println(result)
    //遍历key
//    val result = map.keys
//    for(key<-result){
//       println("key = "+key+",value = "+map.get(key).get)
//    }
    
//    map.foreach(println)
//    println(map)
//    for(elem<-map){
//      println(elem)
//    }
    
    /**
     * set
     */
//    val set = Set[Int](1,2,3,4)
//    val result = set.mkString("@")
//    println(result)
//    val set1 = Set[Int](1,2,6)
//    val result = set1 &~ set
//    result.foreach(println)
    //    for(elem<- set){
//      println(elem)
//    }
    
    /**
     * list
     */
//    val list = List("a","b","c","a","d","a")
//    val result = list.count(s=>{
//      "a".equals(s)
//    })
//    println(result)
//    val list = List[String]("hello bjsxt","hello shsxt","hello gzsxt")
//    val result = list.filter(s=>{
//      s.equals("hello bjsxt")
//    })
//    result.foreach(println)
    
//    val result = list.flatMap(s=>{
//      s.split(" ")
//    })
//    result.foreach(println)
//    val result = list.map(s=>{
//      s.split(" ")
//    })
//    result.foreach(arr=>{
//      println("***")
//      arr.foreach(println)
//    })
    
    
    
//    val list = List[Int](1,2,3,4,5,5)
//    list.foreach(println)
//    for(elem<- list){
//      println(elem)
//    }
    /**
     * array
     */
//    val arr = Array.fill(5)("hello")
//    arr.foreach(println)
//    val array = new Array[Array[String]](3)
//    array(0)=Array[String]("a","b","c")
//    array(1)=Array[String]("d","e","f")
//    array(2)=Array[String]("g","j","h")
//    
//    array.foreach(arr=>{
//      arr.foreach(println)
//    })
    
//    val arr = new Array[Int](3)
//    arr(0)=1
//    arr(1)=100
//    arr(2)=1000
//    arr.foreach(println)
//    val arr = Array[Int](1,2,3,4)
//    arr.foreach(println)
//    for(elem<-arr){
//      println(elem)
//    }
    
  }
}

  

原文地址:https://www.cnblogs.com/huiandong/p/9278289.html