当前位置:网站首页>scala 转换、过滤、分组、排序
scala 转换、过滤、分组、排序
2022-07-28 11:42:00 【但行益事莫问前程】
文章目录
前言
1. map() 将集合内元素按照规则进行转换
为集合中元素,添加前缀
val list = List(1, 2, 4, 9, 3)
val list1 = list.map((num: Int) => {
"00000"+num
})
println(list1.mkString(","))
2. filter() 根据指定的规则对集合内元素进行筛选过滤
val list = List(1, 2, 4, 9, 3)
val list2 = list.filter(num => num % 2 == 1)
println(list2.mkString(","))

3. groupBy() 按照指定的规则对数据进行分组
val list = List(1, 2, 4, 9, 3)
val list3 = list.groupBy(num => num % 3)
println(list3.mkString(","))

4. sortBy() 按照指定的规则对集合的数据进行排序
(1)默认规则为升序,对于字符串采用字典序排序
val list5 = List(1, 4, 3, 2)
val list6 = list5.sortBy(num => num)
println(list6)
val list7 = List("1", "111", "2", "3", "22")
val list8 = list7.sortBy(str => str)
println(list8)
val list9 = list7.sortBy(str => str.toInt)
println(list9)

(2)需要使用降序,可传递第二个参数列表Ordering.Int.reverse
val list5 = List(1, 4, 3, 2)
val list10 = list5.sortBy(num => num)(Ordering.Int.reverse)
println(list10)

(3)对象排序
scala的元组,默认按照第一个排序,第一个相同时,按照第二个排序,依次类推
先按照年龄排序(从小到大),再按照薪水排序(从大到小)
package com.cz.part7
object T23 {
def main(args: Array[String]): Unit = {
val user1 = new User
user1.name = "cz"
user1.age = 18
user1.sal = 8000
val user2 = new User
user2.name = "fd"
user2.age = 39
user2.sal = 10000
val user3 = new User
user3.name = "fe"
user3.age = 5
user3.sal = 20000
val user4 = new User
user4.name = "iop"
user4.age = 18
user4.sal = 7000
val users: List[User] = List(user1, user2, user3, user4)
val users1 = users.sortBy((user: User) => (user.age, user.sal))(Ordering.Tuple2(Ordering.Int, Ordering.Int.reverse))
println(users1)
}
class User {
var age: Int = _
var name: String = _
var sal: Int = _
override def toString: String = s"age:$age name:$name sal:$sal"
}
}

(4)sortWith()自定义排序
val users2 = users.sortWith(
(left, right) => {
if (left.age < right.age) {
true
} else if (left.age == right.age) {
left.sal > right.sal
} else {
false
}
}
)
println(users2)

边栏推荐
- Solve the PHP prompt warning: division by zero in error
- 通过Jenkins 拉取服务器代码 权限不足问题及其他注意事项
- sqli-labs(less-8)
- 开源社区三十年 | 2022 开放原子全球开源峰会开源社区三十年专题活动圆满召开
- Design a thread pool
- Developing NES games with C language (cc65) 11. Metatiles
- MMA8452Q几种模式的初始化实例
- 论治理与创新 | 2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满召开
- 聚变云原生,赋能新里程 | 2022 开放原子全球开源峰会云原生分论坛圆满召开
- SQL injection less24 (secondary injection)
猜你喜欢

DIY system home page, your personalized needs PRO system to meet!

界面控件Telerik UI for WPF - 如何使用RadSpreadsheet记录或评论

聚变云原生,赋能新里程 | 2022 开放原子全球开源峰会云原生分论坛圆满召开

leetcode:704二分查找

FlexPro软件:生产、研究和开发中的测量数据分析

Basic use of JSON server

Arduino Pro Mini atmega328p connect and light the first LED (at the same time, record the problem of burning failure stk500_recv)

Implementation method of mouse hover, click and double click in ue4/5

GMT installation and use

Uniapp wechat applet realizes the function of connecting low-power Bluetooth printing
随机推荐
Developing NES games with C language (cc65) 08. Background collision
Basic use of JSON server
新零售电商O2O模式解析
sqli-labs(less-8)
用C语言开发NES游戏(CC65)02、什么是v-blank?
The openatom openharmony sub forum was successfully held, and ecological and industrial development entered a new journey
Multi Chain and multi currency wallet system development cross chain technology
Did kafaka lose the message
Develop NES game (cc65) 07 and controller with C language (collision with spirit)
Laravel form data validation
Holes in [apue] files
Is it difficult for cloud native machine learning to land? Lingqueyun helps enterprises quickly apply mlops
MarkDown简明语法手册
金九银十 再不卷就来不及了
JSP自定义标签之自定义分页标签02
西门子对接Leuze BPS_304i 笔记
界面控件Telerik UI for WPF - 如何使用RadSpreadsheet记录或评论
SQL injection less23 (filter comment)
HMS core audio editing service supports 7 kinds of audio effects to help one-stop audio processing
Baidu map API adds information window circularly. The window only opens at the last window position and the window information content is the same solution