ConcurrentNavigableMap

java.util.concurrent.ConcurrentNavigableMap Example

In this example we shall show you how to make use ConcurrentNavigableMap Interface, ConcurrentNavigableMap is a NavigableMap which provides navigation methods that returns the closest match for given search targets with a concurrent access support for its submaps. The submaps are the maps returned by various methods like headMap(K toKey), tailMap(K fromKey) and subMap(K fromKey, K toKey).

 

 

 

ConcurrentNavigableMap Methods:

  1. descendingKeySet() :
    Returns a reverse order NavigableSet view of the keys contained in this map.
  2. descendingMap() :
    Returns a reverse order view of the mappings contained in this map.
  3. headMap(K toKey) :
    Returns a view of the portion of this map whose keys are strictly less than toKey.
  4. headMap(K toKey, boolean inclusive) :
    Returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey.
  5. keySet() :
    Returns a NavigableSet view of the keys contained in this map.
  6. navigableKeySet() :
    Returns a NavigableSet view of the keys contained in this map.
  7. subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) :
    Returns a view of the portion of this map whose keys range from fromKey to toKey.
  8. subMap(K fromKey, K toKey) :
    Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive.
  9. tailMap(K fromKey) :
    Returns a view of the portion of this map whose keys are greater than or equal to fromKey.
  10. tailMap(K fromKey, boolean inclusive) :
    Returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey.

Now, let’s see an example which shows some of the previous methods usage.

Example:

ConcurrentNavigableMapDemo.java:

package com.jcg;

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

/**
 * @author ashraf
 *
 */
public class ConcurrentNavigableMapDemo {

	public static void main(String[] args) {

		System.out.println("Example of ConcurrentNavigableMap:\n");

		ConcurrentNavigableMap navmap = new ConcurrentSkipListMap();

		navmap.put(1, "Sunday");

		navmap.put(2, "Monday");

		navmap.put(3, "Tuesday");

		navmap.put(4, "Wednesday");

		navmap.put(5, "Thursday");

		navmap.put(6, "Friday");

		navmap.put(7, "Saturday");

		System.out.println("1. descendingKeySet(): "
				+ navmap.descendingKeySet() + "\n");

		System.out.println("2. descendingMap(): " + navmap.descendingMap()
				+ "\n");

		System.out.println("3. headMap(K toKey): " + navmap.headMap(3) + "\n");

		System.out.println("4. headMap(K toKey, boolean inclusive): "
				+ navmap.headMap(3, true) + "\n");

		System.out.println("5. keySet(): " + navmap.keySet() + "\n");

		System.out.println("6. navigableKeySet(): " + navmap.navigableKeySet()
				+ "\n");

		System.out
				.println("7. subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive): "
						+ navmap.subMap(3, true, 6, true) + "\n");

		System.out.println("8. subMap(K fromKey, K toKey): "
				+ navmap.subMap(3, 6) + "\n");

		System.out
				.println("9. tailMap(K fromKey): " + navmap.tailMap(3) + "\n");

		System.out.println("10. tailMap(K fromKey, boolean inclusive): "
				+ navmap.tailMap(3, true) + "\n");
	}

}

Output:

Example of ConcurrentNavigableMap:

1. descendingKeySet(): [7, 6, 5, 4, 3, 2, 1]

2. descendingMap(): {7=Saturday, 6=Friday, 5=Thursday, 4=Wednesday, 3=Tuesday, 2=Monday, 1=Sunday}

3. headMap(K toKey): {1=Sunday, 2=Monday}

4. headMap(K toKey, boolean inclusive): {1=Sunday, 2=Monday, 3=Tuesday}

5. keySet(): [1, 2, 3, 4, 5, 6, 7]

6. navigableKeySet(): [1, 2, 3, 4, 5, 6, 7]

7. subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive): {3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday}

8. subMap(K fromKey, K toKey): {3=Tuesday, 4=Wednesday, 5=Thursday}

9. tailMap(K fromKey): {3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday}

10. tailMap(K fromKey, boolean inclusive): {3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday}

Download the Source Code of this example:

This was an example of how to use Java ConcurrentNavigableMap.

Download
You can download the full source code of this example here: ConcurrentNavigableMapExampleCode.zip

Ashraf Sarhan

Ashraf Sarhan is a passionate software engineer, an open source enthusiast, has a Bsc. degree in Computer and Information Systems from Alexandria University. He is experienced in building large, scalable and distributed enterprise applications/service in multiple domains. He also has a keen interest in JavaEE, SOA, Agile and Big Data technologies.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button