当前位置:网站首页>十一、Kotlin进阶学习:1、集合;2、List操作;3、可变集合——MutableList;4、Set;5、Map;6、MutableMap;
十一、Kotlin进阶学习:1、集合;2、List操作;3、可变集合——MutableList;4、Set;5、Map;6、MutableMap;
2022-07-30 05:43:00 【¥伊人独醉】
1、集合;
(1)Collection,单值集合
3个子接口:List\Set\MutableColletion
(2)Map,双列集合(key 和 value)
3个子接口:HashMap\TreeMap\MutableMap
【案例】不可变List
初始化:
操作:isEmpty():Boolean 判断是否为空
size:Int 获取元素个数
contains(element: @UnsafeVariance E): Boolean 判断某某元素是否存在
iterator():Iterator,获取迭代器
日志:
【案例】iterator
日志:
2、List——操作;
containsAll(elements:Collection<E>):Boolean 表示某某子集是否在某某集合中
get(index):E 按照index位置,获取一个元素
indexOf(element:@UnsafeVariance E):Int 返回的是元素在集合中首次出现的位置
lastIndexOf(element:@UnsafeVariance E):Int 返回的是元素在集合中最后一次出现的位置
subList(fromIndex:Int ,toIndex:Int):List<E> 表示指定从某某位置开始到某某位置结束的一个子集
【案例】
日志:
3、可变集合——MutableList;
它实现了List<E>和MutableCollection<E>,增加了对集合的添加、替换、删除等操作。
api:
isEmpty():Boolean 判断是否为空集合
val size:Int 返回集合中的元素个数
contains(element:@UnsafeVariance E):Boolean 判断是否包含某某元素
iterator():Iterator<E> 返回集合的迭代器
add(element:E):Boolean 添加某某元素
add(index:Int,element:E):Unit 在指定位置上添加一个元素
remove(element:E):Boolean 移除某某元素,true表示成功,false表示失败
removeAt(index:Int):E 移除指定索引位置的元素,返回这个元素
set(index:Int,element:E) 指定某某位置上的元素换成element
addAll(elements:Collection<E>) :Boolean 向集合中添加一个集合
retainAll(elements:Collection<E>):Boolean 判断某某子集是否在整个集合中,true表示是;false表示否。保留的是交集这个集合。
removeAll(elements:Collection<E>):Boolean 移除集合中一个集合,如果移除成功,返回true;否则返回false
clear():Unit 清空集合
【案例】
日志:
【案例】
retainAll(elements:Collection<E>):Boolean 判断某某子集与整个集合是否有交集,true表示是;false表示否。保留的是交集这个集合。
日志:
4、Set;
List中元素是有序可重复
Set中元素是无序不可重复
kotlin中Set分为两种
(1)Set(不可变)
api:
isEmpty():Boolean 判断是否为空
val size:Int 判断有多少个元素
contains(element:@UnsafeVariance:E):Boolean 判断是否包含某某元素
iterator():Iterator<E> 返回集合的迭代器
containsAll(elements:Collection<E>):Boolean 返回某某子集是否存在
【案例】
(2)MutableSet(可变)
继承了Set接口和MutableCollection接口
可变:add\remove
【案例】
5、Map;
双列集合
存储key和value的值
不可变集合
考虑2个问题:
(1)如何初始化?
mapOf(key to value,key to value……)
to是一个关键字
(2)如何操作?
api:
isEmpty():Boolean 判断集合是否空
val size:Int 获取集合元素个数
containsKey(key:K):Boolean 判断集合是否包含某某key
containsValue(value:V):Boolean 判断集合是否包含某某value
get(key:K):V? 根据key来获取value,如果存在返回对应的value,如果不存在,返回null
迭代
【案例】
6、MutableMap;
可变Map
元素个数可变
可变的操作是什么?
put(key:K,value:V):V? 存入对应key的value
remove(key:K):V? 删除指定key对应的value这个元素
putAll(map:MutableMap<K,V>) 存入一个可变集合
clear(),清除集合所有元素
【案例】
日志:
边栏推荐
- misc-log analysis of CTF
- Trust anchor for certification path not found.异常解决方法。
- shardingsphere 分库分表及配置示例
- 【数仓】数据质量
- Reasons and solutions for Invalid bound statement (not found)
- Use kotlin to extend plugins/dependencies to simplify code (after the latest version 4.0, this plugin has been deprecated, so please choose to learn, mainly to understand.)
- Jdbc & Mysql timeout分析
- 网上说的挖矿究竟是什么? 挖矿系统开发详解介绍
- C#下利用开源NPlot绘制股票十字交叉线
- Using custom annotations, statistical method execution time
猜你喜欢
Solution to TypeError The view function did not return a valid response. The function either returned None
Nacos配置中心用法详细介绍
21. Kotlin Advanced Learning: Implementing Simple Network Access Encapsulation
在不同的服务器上基于docker部署redis主从同步
Jdbc & Mysql timeout analysis
Student achievement management system (C language version)
CTFSHOW command execution [web29-web124] unfinished to be continued
JVM Learning (2) Garbage Collector
awd --waf deployment
SSTI range
随机推荐
SSTI range
CTF misc-audio and video steganography
MySQL - Function and Constraint Commands
[MATLAB] Image Processing - Recognition of Traffic Signs
TDengineGUI无法连接TDengine
MySQL - Multi-table query and case detailed explanation
Function 函数式接口及应用
标准输入输出流(System.in,System.out)
Function functional interface and application
MySQL window function
The number of warehouse 】 data quality
ClickHouse查询语句详解
Trust anchor for certification path not found. Exception solution.
Blind injection, error injection, wide byte injection, stack injection study notes
【MySQL功法】第5话 · SQL单表查询
Using PyQt5 to add an interface to YoloV5 (1)
MySQL - 多表查询与案例详解
protobuf编码及网络通信应用(一)
Mysql 客户端常见异常分析
Go简单实现协程池