本篇内容:ArtTS语言基础类库-容器类库内容的学习(2.10.2)
一、知识储备
线性容器特点对比
名称 | 使用场景 | 特点 |
---|
ArrayList | 需要频繁读取集合中的元素时,推荐使用 | 要求存储位置是一片连续的内存空间,初始容量为10,并动态扩容,每次扩容为原始容量的1.5倍 |
Vector | 和ArrayList类似,满足用户更多场景下的需求 | 同为基于数组实现,多了get/set接口,每次扩容为原始容量的2倍,api9开始不再维护 |
List | 当需要频繁的插入删除时,推荐使用List高效操作 | 单向链表对象,只能通过头节点开始访问到尾节点,在内存中位置不要必须连续, |
LinkedList | 当需要频繁的插入删除时,推荐使用LinkedList高效操作 | 和List相比,此为双向链表结构,可以快速地在头尾进行增删, |
Deque | 需要频繁在集合两端进行增删元素的操作时,推荐使用Deque。 | 双端队列对象,遵循先进先出以及先进后出的规则,双端队列可以分别从队头或者队尾进行访问。要求存储位置是一片连续的空间,初始容量为8,动态扩容,扩容大小是原来容量的2倍。底层采用循环队列实现,入队和出队操作效率都比较高 |
Quque | 先进先出的场景可以使用 | 和Deque相比,只能在一端删除一端增加,其他与deque相同 |
Stack | 先进后出的场景可以使用Stack | 和deque相比,只能在一端删除,另一端插入, |
非线性容器特点对比
名称 | 使用场景 | 特点 |
---|
HashMap | 需要快速存取、删除以及插入键值对数据时,推荐使用HashMap | key唯一,集合中通过key的hash值确定其存储位置,从而快速找到键值对,初始容量为16,动态扩容为原始容量2倍,底层基于HashTable实现,冲突策略采用链地址法。 |
HashSet | 复用不重复的特性,当需要不重复的集合或去重某个集合的时候使用 | 基于HashMap实现,存储元素value唯一,集合中通过value的hash值确定其存储位置,从而快速找到该值。其他与HashMap一样,无序 |
TreeMap | 需要存储有序键值对的场景使用 | key唯一,有序,底层是红黑树。可以进行快速的插入和删除,效率较低 |
TreeSet | 需要存储有序集合的场景使用 | value唯一,value值有序,底层基于红黑树,可以快速的插入和删除 |
LightWeightMap | 当需要存取key-value键值对时推荐使用 | 轻量级HashMap,初始容量为8,动态扩容为原始容量2倍,冲突策略为线性探测法。集合中的key值的查找依赖于hash值以及二分查找算法,通过 一个数组存储hash值,然后遇到 其他的key值以及value值。 |
LightWeightSet | 当需要存取某个集合或是对某个集合去重时,推荐使用 | 轻量级HashSet,初始容量为8,动态扩容为原始容量2倍,集合中的value值 的查找依赖于hash以及二分查找算法,通过一个数组存储hash值,然后映射到其他数组中的value值。冲突策略为线性探测法 |
PlainArray | 当需要存储key值为number类型的键值对时,可以使用 | key只能 是number,初始容量为16,其他与LightWeightMap相同 |
二、效果一览
三、源码剖析
import ArrayList from '@ohos.util.ArrayList'
import Vector from '@ohos.util.Vector'
import Deque from '@ohos.util.Deque'
import Stack from '@ohos.util.Stack'
import List from '@ohos.util.List'
function init() {
let arrayList = new ArrayList()
arrayList.add(1)
arrayList.add(2)
console.error(`${arrayList[0]}`)
arrayList[0] = 3;
console.error(`${arrayList[0]}`)
arrayList.removeByIndex(0)
console.error(`${arrayList[0]}`)
let vector = new Vector() //过时,不建议使用
vector.add([1, 2, 3])
vector.add(1)
console.error(`vector : ${vector[0]}`)
console.error(`vector : ${vector.getFirstElement()}`)
let deque = new Deque()
deque.insertEnd(2)
deque.insertFront(1)
console.error(`deque: ${deque[0]}`)
deque[0] = 'abc'
console.error(`deque: ${deque[0]}`)
let stack = new Stack()
stack.push(1)
stack.push(2)
console.error(`stack: ${stack[0]}`)
stack.pop()
console.error(`stack: ${stack[0]}`)
stack.peek()
console.error(`stack: ${stack[0]}`)
let list = new List()
list.add(1)
list.add('abc')
console.error(`list: ${list.get(0)}`)
}
@Entry
@Component
struct Index {
aboutToAppear() {
init();
}
build() {
Column() {
}
}
}
import HashMap from '@ohos.util.HashMap'
import TreeMap from '@ohos.util.TreeMap'
import LightWeightMap from '@ohos.util.LightWeightMap'
import PlainArray from '@ohos.util.PlainArray'
function init() {
let hashMap = new HashMap()
hashMap.set('a', 123)
hashMap.set(11, 123)
console.error(`hashMap: ${hashMap.hasKey(11)}`)
console.error(`hashMap: ${hashMap.get(11)}`)
let treeMap = new TreeMap()
treeMap.set('a', 123)
treeMap.set(11, 123)
console.error(`treeMap: ${treeMap.get(11)}`)
console.error(`treeMap: ${treeMap.getFirstKey()}`)
console.error(`treeMap: ${treeMap.getLowerKey(11)}`)
let lightWeightMap = new LightWeightMap()
lightWeightMap.set('a', 123)
lightWeightMap.set(11, 123)
console.error(`lightWeightMap: ${lightWeightMap.get(1)}`)
console.error(`lightWeightMap: ${lightWeightMap.getIndexOfKey(11)}`)
let plainArray = new PlainArray()
plainArray.add(1, 123)
plainArray.add(11, 123)
console.error('plainArray: ' + plainArray.get(1))
console.error('plainArray: ' + plainArray.getKeyAt(1))
}
@Entry
@Component
struct Index {
aboutToAppear() {
init();
}
build() {
Column()
}
}