Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ConcurrentNavigableMap implemented as a skip list?

Recently I run into ConcurrentSkipListMap, which is a skip list implementation of ConcurrentNavigableMap. Now I wonder, why it is implemented as a skip list.

Does skip list a "standard" implementation of concurrent navigable maps? What makes skip list particularly good for it?

like image 867
Michael Avatar asked Oct 11 '12 13:10

Michael


People also ask

What is skip list Java?

What is Skip List in Java? A skip list is a data structure that is based on probability. With a linked list, the skip list is used to hold a sorted list of components or data. It enables the pieces or data to be seen efficiently.

What is concurrent skip list map?

The ConcurrentSkipListMap is a scalable implementation of ConcurrentNavigableMap. All the elements are sorted based on natural ordering or by the Comparator passed during it's construction time.


1 Answers

ConcurrentSkipListMap source code has documented the reason.

There are two reasons for taking this approach instead of the usual array-based

  1. Array based implementations seem to encounter more complexity and overhead
  2. We can use cheaper algorithms for the heavily-traversed index lists than can be used for the base lists

Here is the javadoc

 /*
 * This class implements a tree-like two-dimensionally linked skip
 * list in which the index levels are represented in separate
 * nodes from the base nodes holding data.  **There are two reasons
 * for taking this approach instead of the usual array-based**
 * structure: 1) Array based implementations seem to encounter
 * more complexity and overhead 2) We can use cheaper algorithms
 * for the heavily-traversed index lists than can be used for the
 * base lists.  Here's a picture of some of the basics for a
 * possible list with 2 levels of index:
 *
 * Head nodes          Index nodes
 * +-+    right        +-+                      +-+
 * |2|---------------->| |--------------------->| |->null
 * +-+                 +-+                      +-+
 *  | down              |                        |
 *  v                   v                        v
 * +-+            +-+  +-+       +-+            +-+       +-+
 * |1|----------->| |->| |------>| |----------->| |------>| |->null
 * +-+            +-+  +-+       +-+            +-+       +-+
 *  v              |    |         |              |         |
 * Nodes  next     v    v         v              v         v
 * +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+
 * | |->|A|->|B|->|C|->|D|->|E|->|F|->|G|->|H|->|I|->|J|->|K|->null
 * +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+
like image 62
Amit Deshpande Avatar answered Sep 23 '22 09:09

Amit Deshpande