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:
descendingKeySet()
:
Returns a reverse orderNavigableSet
view of the keys contained in this map.descendingMap()
:
Returns a reverse order view of the mappings contained in this map.headMap(K toKey)
:
Returns a view of the portion of this map whose keys are strictly less thantoKey
.headMap(K toKey, boolean inclusive)
:
Returns a view of the portion of this map whose keys are less than (or equal to, ifinclusive
is true)toKey
.keySet()
:
Returns aNavigableSet
view of the keys contained in this map.navigableKeySet()
:
Returns aNavigableSet
view of the keys contained in this map.subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
:
Returns a view of the portion of this map whose keys range fromfromKey
totoKey
.subMap(K fromKey, K toKey)
:
Returns a view of the portion of this map whose keys range fromfromKey
, inclusive, totoKey
, exclusive.tailMap(K fromKey)
:
Returns a view of the portion of this map whose keys are greater than or equal tofromKey
.tailMap(K fromKey, boolean inclusive)
:
Returns a view of the portion of this map whose keys are greater than (or equal to, ifinclusive
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
You can download the full source code of this example here: ConcurrentNavigableMapExampleCode.zip