demo:
public static void main(String[] args) {
ConcurrentSkipListMap<Integer, Integer> concurrentSkipListMap = new ConcurrentSkipListMap<>();
concurrentSkipListMap.put(1, 1);
concurrentSkipListMap.put(2, 1);
concurrentSkipListMap.put(3, 1);
concurrentSkipListMap.put(4, 1);
concurrentSkipListMap.put(5, 1);
concurrentSkipListMap.put(6, 1);
concurrentSkipListMap.put(7, 1);
concurrentSkipListMap.put(8, 1);
concurrentSkipListMap.put(9, 1);
Integer result = concurrentSkipListMap.get(6);
System.out.println(result);
}
以上代码的作用就是在1~9的key值中找到key为6的value值。
最后是否搜到匹配key以及返回结果的关键代码如下:
这代码的作用就是判断搜索的的节点的key值是否和要搜索的key值相等,如果相等,就返回结果,打上了两个断点,接下来测试下。
第一次运行搜索次数:
第一次搜索key为6的节点,做了6次搜索。
第二次运行搜索次数:
第二次搜索key为6的节点,做了3次搜索。
第三次运行搜索次数:
第三次搜索key为6的节点,做了5次搜索。
总结
从三次运行结果来看,在搜索key为6的节点时,搜索的次数是不定的,次数少的自然快,多的就会慢些,所以完全有可能碰到一个糟糕的数据结构影响搜索效率,不过在实际工作中,跳表的表现还是非常好的,而通过以上的运行结果,也能够证明跳表是随机算法了。