java跳表实现
概念:一种有序链表,带有多级索引,查询性能为O(logN)。
运用:Redis,ConcurrentSkipListMap
这里主要参照了leetcode的实现,补充了泛型实现。
public class SkipList<T> {
//当前层级
private int curentLevel = 1;
//最大索引层级
private final static int MAX_LEVEL = 32;
//队列头节点
private final Node<T> HEAD = new Node<>(null, MAX_LEVEL);
//晋升几率
private final static double promote = 0.25d;
private Comparator<T> comparator;
public SkipList() {
}
public SkipList(Comparator<T> comparator) {
this.comparator = comparator;
}
public void add(T value) {
if (value == null) {
throw new NullPointerException();
}
int level = getRandomLevel();
Node