[Java Learning Notes] Notes about Containers

array: 数组不能扩展 按下标访问
T array = new T[]; T: [int char string …]

Collection: 容器类

List: 接口
-List.add() 添加一个元素,默认尾部添加,可指定位置添加
-List.addAll() 添加一组元素,默认尾部添加,可指定位置添加
-List.remove(index/element) 通过index/element移除元素
-List.removeAll(list) 移除list中的所有元素
-Collections.shuffle(List, rand) 打乱
-Collections.sort(List)排序
-Collections.addAll(list, xxxxx) 添加任意形式数据到list中 [元素列表, 数组, Collection]
-List.indexof(element) 找到List中某元素的索引
-List.contains(element) 如果List中有element 返回True, 否则返回False
-List.contains(list) 如果List中包含所有list中元素,返回True, 否则返回False
-List.subList(pos1, pos2) 返回子集, !!!对子集的任何操作都会在原List中体现,反之亦然!!!
-List.retainAll(list) 取交集
-List.toArray() 转换为数组
-List.isEmpty() 判断是否为空
-List.clear() 清空List
ArrayList: List = new ArrayList (); 查询速度快 插入删除慢
LinkedList: List = new LinkedList (); 查询速度慢 插入删除快
Arrays.asList(array) 将一个数组转换为list, 但底层是数组,不能调整尺寸

LinkedList:
-LinkedList.getFirst() LinkedList.element() 返回第一个元素 若List为空则抛出异常
-LinkedList.peek() 返回第一个元素, 若List为空则返回null
-LinkedList.removeFirst() LinkedList.rempve() 移除并返回第一个元素,若List为空则抛出异常
-LinkedList.poll() 移除并返回第一个元素, 若List为空则返回null
-LinkedList.addFirst(element) 将element插入List头部
-LinkedList.add(element) LinkedList.addLast(element) LinkedList.offer(element) 将element插入List尾部
-LinkedList.removeLast() 移除并返回最后一个元素

Stack: 基于LinkedList实现
-Stack.push() 入栈
-Stack.peek() 返回栈顶元素
-Stack.pop() 移除栈顶元素并返回
-Stack.empty() 判断是否为空

Queue: 基于LinkedList实现
-Queue.offer() 插入一个元素到队尾
-Queue.peek() Queue.element() 返回队头
-Queue.poll() Queue.remove() 移除并返回队头

Set:
HashSet: 查找最快 数据存放无规律
-HashSet.add() 添加元素
-HashSet.contains() 查找是否含有某元素
-HashSet.containsAll() 查找是否含有某些元素
-HashSet.remove() 移除某元素
-HashSet.removeAll() 移除所有元素
TreeSet: 按顺序存放,查找费时
LinkedHashSet: 按插入顺序存放

Map:
HashMap:
-HashMap.get(key) 返回key对应的value
-HashMap.put(key, value) 添加数据
-HashMap.containsKey() 查询是否有某Key
-HashMap.containsValue() 查询是否有某Value
-HashMap.keySet() 返回由Key组成的Set
-HashMap.values() 返回由value组成的Collection

Iterator: 迭代器
Iterator it = Collection .iterator()
-it.hasNext() 检查是否还有元素
-it.next() 获得下一元素
-it.remove() 删除当前元素
关于it.remove()的理解:
一直在想it.remove之后,it指向的是什么位置,通过阅读源码(ArrayList的源码)发现,it维护一个cursor变量存放下一个元素索引位置,和一个lastRet变量表示最后一个被return的元素的索引位置,初始化为-1,当元素被remove后,也将其置为-1,表明已被删除。

那么为什么it.remove()一个元素之后,调用it.next()仍然能正确找到下一个元素呢?

因为it.remove调用的是ArrayList.this.remove()函数,然后令cursor = lastRet, lastRet = -1, 且ArrayList.this.Remove()采用的是System.arraycopy()方法,将需删除元素索引之后的元素依次copy到以需删除元素索引为开始的位置上,并将最后空出来的位置设为NULL,也就是说在删除一个元素之后,后面的元素自动向前补齐。又因为,前面已经将下一个返回的元素的索引位置cursor赋值为上一个返回元素的索引位置lastRet了,所以it.next()仍然能够继续下去。

ListIterator: 可双向移动的迭代器
ListIterator it = Collection . listIterator()
-it.set() 修改当前位置元素值
-it.add() 添加元素
-it.hasPrevious() 检查是否前面有元素
-it.previous() 向前移动
-it.previousIndex() 获得前一位置索引
-it.nextIndex() 获得后一位置索引

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值