Scala集合

博客介绍了数组、List、Set、Map和Tuple的相关知识。包括数组、List、Set、Map的创建、遍历方式及方法,还提及了可变长的情况;对于Map,介绍了合并及过滤等操作;对于Tuple,说明了定义、创建、取值、遍历方式及相关方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.数组

创建数组的两种方式

  • 直接Array一个指定类型的数组并赋值
  • 直接new一个定长的Array[ ]
    //创建类型为Int 长度为3的数组
    val arr1 = new Array[Int](3)
    //创建String 类型的数组,直接赋值
    val arr2 = Array[String]("s100","s200","s300")

遍历数组的两种方式

  • for
  • foreach
for (i <- arr1) {
    println(i)
}

arr1.foreach(i => {
    println(i)
})

可变长数组

val arr = ArrayBuffer[String]("a", "b", "c")
arr.append("hello", "scala") //添加多个元素
arr.+=("end") //在最后追加元素
arr.+=:("start") //在开头添加元素
arr.foreach(println)

2.list

创建list

val list = List(1,2,3,4,5)
  • Nil为数组长度为0的list

遍历list

  • foreach
  • for
//foreach
list.foreach {
    x => println(x)
}

list.foreach(println)

//for
for (i<-list){
    println(i)
}

list的方法

/**
  * filter:过滤元素
  */
val list1 = list.filter { x => x > 3 }
list1.foreach(println)

/**
  * count:计算符合各条件的元素个数
  */
val value = list1.count(x => x > 3)
println(value)

/**
  * map:对元素操作
  */
val nameList = List("zhang san", "li si", "wang wu")
val mapResult: List[Array[String]] = nameList.map(x => x.split(" "))
mapResult.foreach(x => x.foreach(println))

/**
  * flatmap:压扁扁平,先map再flat
  */
val flatMapResult: List[String] = nameList.flatMap(x => x.split(" "))
flatMapResult.foreach(println)

可变长list

val listBuffer: ListBuffer[Int] = ListBuffer[Int](1, 2, 3, 4, 5)
listBuffer.append(6, 7, 8, 9) //追加元素
listBuffer.+=(10) //在后面追加元素
listBuffer.+=:(100) //在开头加入元素
listBuffer.foreach(println)

3.Set

创建Set

  • 注意:set集合会自动去重
/**
  * 创建 
  */
val set1 = Set(1, 2, 3, 4, 4)
val set2 = Set(1, 2, 5)

set的遍历

  • for
  • foreach
set.foreach(println)

for (s <- set) {
  println(s)
}

set的方法

/**
  * 交集:intersect,&
  */
val set3 = set1.intersect(set2)
set3.foreach(println)

val set4 = set1.&(set2)
set4.foreach(println)

/**
  * 差集:diff,&~
  */
set1.diff(set2).foreach(println)
set1.&~(set2).foreach(println)

/**
  * 子集:subsetOf
  */
println(Set(1, 2).subsetOf(Set(1, 2, 3)))

/**
  * 最大最小值:max min
  */
println(set1.max)
println(set1.min)

/**
  * 转成数组,list:toArray toList
  */
set1.toArray.foreach(println)
set1.toList.foreach(println)

/**
  * 转化成字符串:mkString
  */
println(set1.mkString)
println(set1.mkString("\t"))

可变长set

val set = Set[Int](1, 2, 3, 4, 5)
set.add(100)
set.+=(200)
set.+=(1, 210, 300)
set.foreach(println)

4.map

map创建

/**
  * 创建
  */
val map = Map("1" -> "a", "2" -> "b", ("3", "c"))

map获取和遍历

/**
  * 获取值,getOrElse():给一个默认值
  */
println(map.get("1").get)
println(map("1"))

println(map.getOrElse("8", "no value"))
println(map.get("8").getOrElse("no value"))

/**
  * 遍历
  */
for (x <- map) println("====key:" + x._1 + ",value:" + x._2)
map.foreach(f => println("key:" + f._1 + " ,value:" + f._2))

map.keys.foreach(k => println("key:" + k + ",value:" + map(k)))
map.values.foreach(v => println("value:" + v))

合并map

  • map1 ++ map2 :往map1里加入map2
  • map1 ++: map2:往map2里加入map1
val map1 = Map((1, "a"), (2, "b"), (3, "c"))
val map2 = Map((1, "aa"), (2, "bb"), (2, 90), (4, 22), (4, "dd"))

map1.++:(map2).foreach(println)
map1.++(map2).foreach(println)

map的方法

  • filter:过滤,留下符合条件的记录
  • count:统计符合条件的记录数
  • contains:map中是否包含某个key
  • exist:符合条件的记录存在不存在
val countResult = map.count(p => {
  p._2.equals("a")
})
println(countResult)

map.filter(x => x._2.equals("a")).foreach(println)

println(map.contains("2"))

println(map.exists(f => f._2.equals("a")))

可变长map

val map = Map[String,Int]()
map.put("hello",100)
map.put("world",200)
map.foreach(println)

5.Tuple

元组的定义

  • 与列表一样,与列表不同的是元组可以包含不同类型的元素。元组的值是通过将单个的值包含在圆括号中构成的。

创建元组和取值

  • val tuple = new Tuple ( 1 ) 可以使用new
  • val tuple2 = Tuple ( 1, 2 ) 可以不使用new , 也可以直接写成val tuple3 = ( 1, 2, 3 )
  • 取值用 ”._XX ” 可以获取元组中的值
  • 注意 : tuple最多支持22个参数
//创建,最多支持22个
val tuple = new Tuple1(1)
val tuple2 = Tuple2("zhangsan", 2)
val tuple3 = Tuple3(1, 2, 3)
val tuple4 = (1, 2, 3, 4)
val tuple18 = Tuple18(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18)
val tuple22 = new Tuple22(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22)

//使用
println(tuple2._1 + "\t" + tuple2._2)
val t = Tuple2((1, 2), ("zhangsan", "lisi"))
println(t._1._2)

元组的遍历

  • tuple.productIterator得到迭代器,进而遍历
//遍历
val tupleIterator = tuple22.productIterator
while (tupleIterator.hasNext) {
  println(tupleIterator.next())
}

swap,toString 方法

  • swap元素翻转,只针对二元组
//翻转,只针对二元组
println(tuple2.swap)

//toString
println(tuple3.toString())

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值