当一个List拥有快速访问功能时,其遍历方法采用for循环最快速。而没有快速访问功能的List,遍历的时候采用Iterator迭代器最快速。
当我们不明确获取到的是Arraylist,还是LinkedList的时候,我们可以通过RandomAccess来判断其是否支持快速随机访问,若支持则采用for循环遍历,否则采用迭代器遍历。
ArrayList 其实就是一个动态数组。优势就是将数组的操作封装起来,比如增删查,以及动态扩容, 每次1.5倍:int newCapacity = oldCapacity + (oldCapacity >> 1);
,同时还会防止溢出进行比较MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8
。
ArrayList中的线程安全问题:size成员变量问题,设置值得时候的原子性操作问题elementData[size++] = e;
元素减少,缩容:elementData[--size] = null; // clear to let GC do its work
,trimToSize()
ArrayList.subList() 方法返回的List是ArrayList中某段数据的一个视图. 因此, 在操作此方法返回的List时, 同样会改变ArrayList的数据,同样在CopyOnWriteArrayList也存在这个问题,获取到一个远列表的试图,因此取subList时需要考虑线程安全问题,更好的方式是使用不可变列表(例如Guava的ImmutableList),避免对原始列表的更新导致子列表抛出异常
Fail-Fast机制只能用作bug判断,并不是一个强保证。也好理解,其实多线程环境下也不是一定会触发抛ConcurrentModificationException异常的条件
Fail-Safe任何对集合结构的修改都会在一个复制的集合上进行修改,但是会有一下问题 需要复制集合,产生大量的无效对象,开销大;无法保证读取的数据是目前原始数据结构中的数据
Vector和ArrayList基本一样,线程安全;Vector可以设置capacityIncreament(容量增长量),而ArrayList不可以
CopyOnWriteArrayList的几个特点:thread-safe:对于引起list变化(内容或者结构)都是使用ReentrantLock加锁,然后通过底层的数组copy完成的。虽然可以保证安全,但是觉得效率低呀,需要额外的复制数据的操作,只适合读操作远多于写操作的场景。
完美二叉树Perfect Binary Tree(PBT),国内一般称之为满二叉树,所有的叶子节点具有相同的深度。完美(Perfect)二叉树一定是完全(Complete)二叉树,但完全(Complete)二叉树不一定是完美(Perfect)二叉树;完美(Perfect)二叉树一定是完满(Full)二叉树,但完满(Full)二叉树不一定是完美(Perfect)二叉树
完全二叉树Complete Binary Tree (CBT),从根结点到倒数第二层满足完美二叉树,最后一层可以不完全填充,其叶子结点都靠左对齐。
完满二叉树Full Binary Tree (FBT),所有非叶子结点的度都是2。(只要你有孩子,你就必然是有两个孩子)
平衡二叉树In computer science, an AVL tree (named after inventors Adelson-Velsky and Landis) is a self-balancing binary search tree,当且仅当任何节点的两棵子树的高度差不大于1的二叉树;
霍夫曼树 Huffman Tree
B-Tree,To understand the use of B-Trees, we must think of the huge amount of data that cannot fit in main memory. When the number of keys is high, the data is read from disk in the form of blocks. Disk access time is very high compared to the main memory access time. The main idea of using B-Trees is to reduce the number of disk accesses. Most of the tree operations (search, insert, delete, max, min, ..etc ) require O(h) disk accesses where h is the height of the tree. B-tree is a fat tree. The height of B-Trees is kept low by putting maximum possible keys in a B-Tree node. Generally, the B-Tree node size is kept equal to the disk block size. Since the height of the B-tree is low so total disk accesses for most of the operations are reduced significantly compared to balanced Binary Search Trees like AVL Tree, Red-Black Tree, ..etc
All nodes (including root) may contain at most 2t – 1 keys?????
Number of children of a node is equal to the number of keys in it plus 1
All keys of a node are sorted in increasing order. The child between two keys k1 and k2 contains all keys in the range from k1 and k2
B-Tree grows and shrinks from the root which is unlike Binary Search Tree. Binary Search Trees grow downward and also shrink from downward.?????
Like other balanced Binary Search Trees, time complexity to search, insert and delete is O(log n).
你要不会唠嗑能不能别硬唠。--[缝纫机乐队]
> 可在下面留言(需要有 GitHub 账号)