It’s very important to differentiate between java.util.TreeMap and java.util.concurrent.ConcurrentSkipListMap, so in this Collection framework tutorial we will learn what are differences and similarities between java.util.TreeMap and java.util.concurrent.ConcurrentSkipListMap in java.
Contents of page :
- Differences between java.util.TreeMap and java.util.concurrent.ConcurrentSkipListMap in java >
- Similarity between java.util.TreeMap and java.util.concurrent.ConcurrentSkipListMap in java >
- Program 1 to show
- Program 2 to show
- The iterators returned by the iterator() method of ConcurrentSkipListMap are fail-safe in java >
Property
|
java.util.TreeMap
|
java.util.concurrent. ConcurrentSkipListMap
| |
1
|
synchronization
|
TreeMap is not synchronized (because 2 threads on same TreeMap object can access it at same time) in java.
|
ConcurrentSkipListMap is synchronized (because 2 threads on same ConcurrentSkipListMap object cannot access it at same time) in java.
|
2
|
Iterator
|
all three iterators are fail-fast, means any structural modification made to TreeMap during iteration using any of 3 Iterator will throw ConcurrentModificationException.
As shown in Program 1 below.
|
The iterators returned by the iterator() method of Map's “collection view methods" are fail-safe >
all three iterators are fail-safe.
As shown in Program 2 below.
|
3
|
Performance
|
TreeMap is not synchronized, hence its operations are faster as compared to ConcurrentSkipListMap.
|
ConcurrentSkipListMap is synchronized, hence its operations are slower as compared to TreeMap.
|
4
|
Introduced inin which java version
|
TreeMap was introduced in second version of java i.e. JDK 2.0
|
ConcurrentSkipListMap was introduced in sixth version of java i.e. JDK 6.0
|
5
|
Package
|
java.util
|
java.util.concurrent
|
6
|
Implements which interface
|
Map
SortedMap
NavigableMap
|
Map
SortedMap
NavigableMap
ConcurrentNavigableMap
|
So far we have learned what are differences between java.util.TreeMap and java.util.concurrent.ConcurrentSkipListMap in java.
Now we will learn similarities in java.util.TreeMap and java.util.concurrent.ConcurrentSkipListMap in Collection framework in java.
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
Similarity between TreeMap and ConcurrentSkipListMap in java>
Property
|
java.util.TreeMap and java.util.concurrent.ConcurrentSkipListMap
| |||||||||
1
|
Insertion order
|
TreeMap and ConcurrentSkipListMap both are sorted by natural order of keys in java.
| ||||||||
2
|
TreeMap and ConcurrentSkipListMap both does not allow to store null key but allow many null values in java.
Any attempt to store null key throws runtimeException (NullPointerException).
| |||||||||
3
|
Implements java.util.Map
| |||||||||
4
|
Complexity
|
Complexity offered by methods of TreeMap and ConcurrentSkipListMap both of these is same in java.
|
Example/Program 1 to show
The iterators returned by the iterator() method of TreeMap are fail-fast >
- map.keySet().iterator()
- map.values().iterator()
- map.entrySet().iterator()
all three iterators are fail-fast, means any structural modification made to TreeMap during iteration using any of 3 Iterator will throw ConcurrentModificationException.
Also, iteration done on TreeMap using enhanced for loop is Fail-fast in java.
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapExample {
public static void main(String args[]) {
Map<Integer, String> treeMap = new TreeMap<Integer, String>();
treeMap.put(11, "audi");
treeMap.put(21, "bmw");
treeMap.put(31, "ferrari");
//fail-fast
Iterator<Integer> keyIterator = treeMap.keySet().iterator();
while (keyIterator.hasNext()) {
treeMap.put(4, "porsche");//unComment to avoid ConcurrentModificationException
System.out.println(keyIterator.next());
}
//enhanced for loop on keys is fail-fast
Set<Integer> keySet = treeMap.keySet();
for (Integer key : keySet) {
treeMap.put(4, "porsche");//unComment to avoid ConcurrentModificationException
System.out.println(key);
}
//fail-fast
Iterator<String> valueIterator = treeMap.values().iterator();
while (valueIterator.hasNext()) {
treeMap.put(4, "porsche");//unComment to avoid ConcurrentModificationException
System.out.println(valueIterator.next());
}
//enhanced for loop on values is fail-fast
Collection<String> collection = treeMap.values();
for (String value : collection) {
treeMap.put(4, "porsche");//unComment to avoid ConcurrentModificationException
System.out.println(value);
}
//fail-fast
Iterator<Entry<Integer, String>> entryIterator = treeMap.entrySet()
.iterator();
while (entryIterator.hasNext()) {
treeMap.put(4, "porsche");//unComment to avoid ConcurrentModificationException
System.out.println(entryIterator.next());
}
//enhanced for loop on entry is fail-fast
Set<Entry<Integer, String>> entrySet = treeMap.entrySet();
for (Entry<Integer, String> entry : entrySet) {
treeMap.put(4, "porsche");//unComment to avoid ConcurrentModificationException
System.out.println(entry);
}
}
}
|
In above program structural modification was made to TreeMap during iteration, and that didn’t throwed ConcurrentModificationException.
Example/Program 2 to show
The iterators returned by the iterator() method of ConcurrentSkipListMap are fail-safe >
- map.keySet().iterator()
- map.values().iterator()
- map.entrySet().iterator()
all three iterators are fail-safe.
Also, iteration done on ConcurrentSkipListSet using enhanced for loop is Fail-safe in java.
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListMap;
public class ConcurrentSkipListMapExample {
public static void main(String args[]) {
Map<Integer, String> concurrentSkipListMap = new ConcurrentSkipListMap<Integer, String>();
concurrentSkipListMap.put(11, "audi");
concurrentSkipListMap.put(21, "bmw");
concurrentSkipListMap.put(31, "ferrari");
// fail-safe
Iterator<Integer> keyIterator = concurrentSkipListMap.keySet()
.iterator();
while (keyIterator.hasNext()) {
System.out.println(keyIterator.next());
}
// enhanced for loop on keys is fail-safe
Set<Integer> keySet = concurrentSkipListMap.keySet();
for (Integer key : keySet) {
concurrentSkipListMap.put(4, "porsche");
System.out.println(key);
}
// fail-safe
Iterator<String> valueIterator = concurrentSkipListMap.values()
.iterator();
while (valueIterator.hasNext()) {
concurrentSkipListMap.put(5, "porsche");
System.out.println(valueIterator.next());
}
// enhanced for loop on values is fail-safe
Collection<String> collection = concurrentSkipListMap.values();
for (String value : collection) {
concurrentSkipListMap.put(6, "porsche");
System.out.println(value);
}
// fail-safe
Iterator<Entry<Integer, String>> entryIterator = concurrentSkipListMap
.entrySet().iterator();
while (entryIterator.hasNext()) {
concurrentSkipListMap.put(7, "porsche");
System.out.println(entryIterator.next());
}
// enhanced for loop on entry is fail-safe
Set<Entry<Integer, String>> entrySet = concurrentSkipListMap.entrySet();
for (Entry<Integer, String> entry : entrySet) {
concurrentSkipListMap.put(8, "porsche");
System.out.println(entry);
}
}
}
|
In above program structural modification was made to ConcurrentSkipListMap during iteration, but that didn’t throw ConcurrentModificationException in java.
So in this Collection framework tutorial we learned what are differences and similarities between java.util.TreeMap and java.util.concurrent.ConcurrentSkipListMap in java.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>
Map hierarchy tutorial in java - Detailed - java.util.HashMap, java.util.Hashtable, java.util.concurrent.ConcurrentHashMap, java.util.LinkedHashMap, java.util.TreeMap, java.util.concurrent.ConcurrentSkipListMap, java.util.IdentityHashMap, java.util.WeakHashMap, java.util.EnumMap classes
Map Differences >
HashMap and Hashtable - Similarity and Differences in java
HashMap and ConcurrentHashMap - Similarity and Differences in java
HashMap vs Hashtable vs LinkedHashMap vs TreeMap - Differences in java
HashMap vs IdentityHashMap - Similarity and Differences with program in java
Important Similarity and Differences Collection classes in concurrent and non-concurrent packages in java >
HashSet vs CopyOnWriteArraySet in java - Similarity and Differences with program
TreeSet vs ConcurrentSkipListSet in java - Similarity and Differences with program
Top Collection Interviews question and answers >
COLLECTION - Top 50 interview questions and answers in java for fresher and experienced
COLLECTION - Top 50 interview questions and answers in java for fresher and experienced