Java ConcurrentNavigableMap Concurrent Collection

Category: Java   Tags: Java, Java Thread, Java MultiThread, Thread Synchronization, Immutable Objects, Java Concurrent Collection

ConcurrentNavigableMap is a ConcurrentMap that support NavigableMap operations.

Methods

Here we are mentioning some import methods of ConcurrentMap:

ConcurrentNavigableMap headMap(K key)
It will return a view of the portion of this map whose keys are less than key. Changes in the returned map are reflected in this map, and vice-versa.
ConcurrentNavigableMap headMap(K key, boolean inclusive)
It will return a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) key. Changes in the returned map are reflected in this map, and vice-versa.
ConcurrentNavigableMap tailMap(K key)
It will return a view of the portion of this map whose keys are greater than or equal to key. Changes in the returned map are reflected in this map, and vice-versa.
ConcurrentNavigableMap tailMap(K key, boolean inclusive)
It will return a view of the portion of this map whose keys are greater than or equal to (if inclusive is true) key. Changes in the returned map are reflected in this map, and vice-versa.
ConcurrentNavigableMap subMap(K startKey, K endKey)
It will return a view of the portion of this map whose keys range from startKey (inclusive) to endKey (exclusive). Changes in the returned map are reflected in this map, and vice-versa.
ConcurrentNavigableMap descendingMap()
Returns a reverse order view of this map. Changes in the returned map are reflected in this map, and vice-versa.
NavigableSet descendingKeySet()
Returns a reverse order view of the keys contained in this map. Changes in the returned map are reflected in this map, and vice-versa.

Here is a simple Java example of ConcurrentNavigableMap using ConcurrentSkipListMap:

ConcurrentSkipListMapExample.java

                            package com.tutorial.javabasic;

                            import java.util.Iterator;
                            import java.util.NavigableSet;
                            import java.util.concurrent.ConcurrentNavigableMap;
                            import java.util.concurrent.ConcurrentSkipListMap;

                            class ConcurrentSkipListMapExample {

                                public static void main(String s[]) {
                                    ConcurrentSkipListMap<Integer, String> concurrentSkipListMap = new ConcurrentSkipListMap<Integer, String>();
                                    concurrentSkipListMap.put(1, "A");
                                    concurrentSkipListMap.put(2, "B");
                                    concurrentSkipListMap.put(3, "C");
                                    concurrentSkipListMap.put(4, "D");

                                    NavigableSet keySet = concurrentSkipListMap.keySet();

                                    System.out.println("Keys are: ");
                                    for (Iterator itr = keySet.iterator(); itr.hasNext();) {
                                        System.out.println(itr.next());
                                    }

                                    ConcurrentNavigableMap<Integer, String> tailMap = concurrentSkipListMap.tailMap(2);

                                    System.out.println("Elements having key greater than equal to 2:");
                                    NavigableSet tailMepKeySet = tailMap.keySet();

                                    for (Iterator tailMapIterator = tailMepKeySet.iterator(); tailMapIterator.hasNext();) {
                                        System.out.println(tailMapIterator.next());
                                    }

                                    // Creates a submap starting from key 1 to 3
                                    ConcurrentNavigableMap<Integer, String> subMap = concurrentSkipListMap.subMap(1, 3);

                                    NavigableSet subMapKeySet = subMap.keySet();

                                    System.out.println("Keyset of sub map having keys 1-3:");

                                    for (Iterator itr = subMapKeySet.iterator(); itr.hasNext();) {
                                        System.out.println(itr.next());
                                    }

                                    ConcurrentNavigableMap<Integer, String> headerMap = concurrentSkipListMap.headMap(3);

                                    System.out.println("Elements having key less than 3:");

                                    NavigableSet headerMapKeySet = headerMap.keySet();

                                    for (Iterator itr = headerMapKeySet.iterator(); itr.hasNext();) {
                                        System.out.println(itr.next());
                                    }

                                    ConcurrentNavigableMap<Integer, String> descendingMap = concurrentSkipListMap.descendingMap();
                                    System.out.println("Elements in descending order of key:");

                                    NavigableSet descendingMapKeySet = descendingMap.keySet();

                                    for (Iterator itr = descendingMapKeySet.iterator(); itr.hasNext();) {
                                        System.out.println(itr.next());
                                    }
                                }
                            }
                        

Output:

                            Keys are:
                            1
                            2
                            3
                            4
                            Elements having key greater than equal to 2:
                            2
                            3
                            4
                            Keyset of sub map having keys 1-3:
                            1
                            2
                            Elements having key less than 3:
                            1
                            2
                            Elements in descending order of key:
                            4
                            3
                            2
                            1