实现 ConcurrentSkipListMap API 的Java程序
ConcurrentSkipListMap API 基于 ConcurrentNavigableMap 的实现。该地图根据其键的自然顺序或在创建期间地图上提供的比较器进行排序。此类实现了 SkipLists 的并发变体,这使得其对 containsKey、get、put、remove 操作及其变体的 log(n) 的预期平均时间成本。
此类及其视图和迭代器实现了 Map 和 Iterator 接口的所有可选方法。与大多数其他并发集合一样,此 API 不允许使用空键或值。它是Java集合框架的成员。
所有实现的接口:
Serializable, Cloneable, ConcurrentMap, ConcurrentNavigableMap, Map,
NavigableMap, SortedMap
句法:
public class ConcurrentSkipListMap
extends AbstractMap
implements ConcurrentNavigableMap, Cloneable, Serializable
构造函数:
- ConcurrentSkipListMap() – 创建一个新的空映射,根据键的自然顺序排序。
- ConcurrentSkipListMap(Comparator super K> c) – 根据给定的比较器创建一个新的空映射。
- ConcurrentSkipListMap(Map extends K,? extends V> mp) – 创建一个与给定 map 具有相同映射的新映射,根据键的自然顺序排序。
- ConcurrentSkipListMap(SortedMap
mp) – 创建一个与给定映射具有相同映射并根据给定排序映射排序的新映射。
方法:Method Description ceilingEntry(K key) This method returns a key-value pair associated with the least key, greater than or equal to the specified key otherwise null. ceilingKey(K key) This method returns the least key greater than or equal to the specified key otherwise null. clear() This method removes all the key-value pairs from the map clone() This method returns a copy of the ConcurrentSkipListMap instance. comparator() This method returns the comparator used for the ordering of the keys in the map otherwise null if it uses the natural ordering for the keys. containsKey(Object key) This method returns true if the map contains the key-value pair for the given key. containsValue(Object value) This method returns true if the map maps one or more keys for the specified value. descendingKeySet() This method returns a reverse order NavigableSet view of the keys of the map descendingMap() This method returns the reverse order view of the key-value pairs contained in the map. entrySet() This method returns a set view of the key-value pairs contained in the map. equals(Object o) This method compares the given object with the map for equality. firstEntry() This method returns a key-value pair associated with the least key in the map. firstKey() This method returns the first(lowest) key in the map floorEntry(K key) This method returns a key-value mapping associated with the greatest key less than or equal to the given key or otherwise null get(Object key) This method returns the value to which the specified key is mapped or otherwise null isEmpty() This method returns true if the map contains no key-value pair in the map keySet() This method returns a NavigableSet view of the keys contained in this map. lastEntry() This method returns a key-value pair associated with the greatest key in the map or otherwise null if the map is empty lastKey() This method returns the last (highest) key currently in the map. headMap(K toKey) This method returns a view of the portion of the map whose keys are less than toKey. lowerKey(K key) This method returns the greatest key strictly less than the given key, or null if there is no such key. size() This method returns the number of key-value pairs in the map tailMap(K fromKey) This method returns a view of the portion of the map whose keys are greater than or equal to fromKey. values() This method returns a collection view of the values in the map remove(Object key) This method removes the mapping for the specified key from the map. put(K key, V value) This method associates the given value with the given key in the map. replace(K key, V value) This method replaces the value of the given key with the specified value if it is mapped with some other value.
Java
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NavigableSet;
import java.util.Set;
import java.util.SortedMap;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
public class ConcurrentSkipList {
private ConcurrentSkipListMap cmp;
// Creates a new empty map sorted according to the
// natural ordering of the keys
public ConcurrentSkipList()
{
cmp = new ConcurrentSkipListMap();
}
// Creates a new empty map sorted according to the gievn
// comparator
public ConcurrentSkipList(Comparator super K> comp)
{
cmp = new ConcurrentSkipListMap(comp);
}
// Creates a new map with the same key-value pairs as
// the specified map and sorted according to the natural
// ordering of the keys.
public ConcurrentSkipList(
Map extends K, ? extends V> mp)
{
cmp = new ConcurrentSkipListMap(mp);
}
// Creates a new map containing the same key-value pairs
// and same ordering as the specified map.
public ConcurrentSkipList(SortedMap mp)
{
cmp = new ConcurrentSkipListMap(mp);
}
// Returns a mapping associated with the least key
// greater than or equal to the specified key or
// otherwise null
public Map.Entry ceilingEntry(K k)
{
return cmp.ceilingEntry(k);
}
// Returns the least key greater than or equal to the
// specified key or otherwise null
public K ceilingKey(K k) { return cmp.ceilingKey(k); }
// Removes all the key-value pairs of the map
public void clear() { cmp.clear(); }
// Returns a copy of the ConcurrentSkipListMap
public ConcurrentSkipListMap clone()
{
return cmp.clone();
}
// Returns the comparator used for ordering the keys in
// the map
public Comparator super K> comparator()
{
return cmp.comparator();
}
// Returns true if the map contains the specified key.
public boolean containsKey(Object k)
{
return cmp.containsKey(k);
}
// Returns true if the map contains one or more keys
// mapped to the given value
public boolean containsValue(Object v)
{
return cmp.containsValue(v);
}
// Returns a reverse order NavigableSet view of the keys
// in the map
public NavigableSet descendingKeySet()
{
return cmp.descendingKeySet();
}
// Returns a reverse order view of the key-value pairs
// in the map.
public ConcurrentNavigableMap descendingMap()
{
return cmp.descendingMap();
}
// Returns a Set view of the key-value pairs in the map
public Set > entrySet()
{
return cmp.entrySet();
}
// Returns the mapping that is associated with the least
// key in the map or otherwise null
public Map.Entry firstEntry()
{
return cmp.firstEntry();
}
// Returns the first (lowest) key in the map.
public K firstKey() { return cmp.firstKey(); }
// Returns the greatest key less than or equal to the
// specified key or otherwise null
public K floorKey(K k) { return cmp.floorKey(k); }
// Returns the value to which the given key is mapped
public V get(Object k) { return cmp.get(k); }
// Returns a portion of the map whose keys are strictly
// less than k
public ConcurrentNavigableMap headMap(K k)
{
return cmp.headMap(k);
}
// Returns a portion of the map whose keys are strictly
// less than or equal to k
public ConcurrentNavigableMap headMap(K k,
boolean in)
{
return cmp.headMap(k, in);
}
// Returns a mapping that is associated with the least
// key strictly greater than the specified key or
// otherwise null
public Map.Entry higherEntry(K k)
{
return cmp.higherEntry(k);
}
// Returns the least key strictly greater than the given
// key, or otherwise null.
public K higherKey(K k) { return cmp.higherKey(k); }
// Returs the set view of the keys in the map
public Set keySet() { return cmp.keySet(); }
// Returns a mapping associated with the greatest key in
// the map, or otherwise null.
public Map.Entry lastEntry()
{
return cmp.lastEntry();
}
// Returns the last(highest) key in the map.
public K lastKey() { return cmp.lastKey(); }
// Returns a mapping that is associated with the
// greatest key strictly less than the specified key or
// otherwise null
public Map.Entry lowerEntry(K k)
{
return cmp.lowerEntry(k);
}
// Returns the greatest key strictly less than the given
// key or otherwise null
public K lowerKey(K k) { return cmp.lowerKey(k); }
// Returns a NavigableSet view of the keys in the map.
public NavigableSet navigableKeySet()
{
return cmp.navigableKeySet();
}
// Removes and returns a mapping associated with the
// least key in the map, or otherwise null
public Map.Entry pollFirstEntry()
{
return cmp.pollFirstEntry();
}
// Removes and returns a mapping associated with the
// greatest key
// in the map, or otherwise null
public Map.Entry pollLastEntry()
{
return cmp.pollLastEntry();
}
// Maps the given value to the given key in the map
public V put(K k, V v) { return cmp.put(k, v); }
// Copies all the key-value pairs of the given map to
// the ConcurrentSkipListMap
public void putAll(Map extends K, ? extends V> mp)
{
cmp.putAll(mp);
}
// Removes the mapping of the given key from the map
public V remove(Object k) { return cmp.remove(k); }
// Replaces the given key with the given value if the
// key is already mapped
public V replace(K k, V v) { return cmp.replace(k, v); }
// Replaces the given key with the given value if the
// key is already mapped
public boolean replace(K k, V oValue, V nValue)
{
return cmp.replace(k, oValue, nValue);
}
// Returns the number of mapping in the map
public int size() { return cmp.size(); }
// Return a portion of the map whose keys are from k1 to
// k2
public ConcurrentNavigableMap
subMap(K k1, boolean f, K k2, boolean t)
{
return cmp.subMap(k1, f, k2, t);
}
// Returns a portion of the map whose keys are from
// k1(inclusive) to k2(exclusive)
public ConcurrentNavigableMap subMap(K k1, K k2)
{
return cmp.subMap(k1, k2);
}
// Returns a Collection view of the values of the map
public Collection values() { return cmp.values(); }
public static void main(String[] arg)
{
ConcurrentSkipList cmp
= new ConcurrentSkipList();
cmp.put(1, "Sahil");
cmp.put(2, "Kahish");
cmp.put(3, "Tarun");
cmp.put(4, "Karan");
Map mp
= new HashMap();
mp.put(5, "Shweta");
mp.put(6, "Aditya");
cmp.putAll(mp);
System.out.println("The key set of the map is ");
Set kSet = cmp.keySet();
Iterator i = kSet.iterator();
while (i.hasNext()) {
System.out.print(i.next() + " ");
}
System.out.println();
System.out.println("The values of the Map is ");
Collection values = cmp.values();
Iterator it = values.iterator();
it = values.iterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println();
System.out.println(
"Poll first entry of the ConcurrentSkipListMap ");
Map.Entry pfe
= cmp.pollFirstEntry();
System.out.println("Key = " + pfe.getKey()
+ " Value = " + pfe.getValue());
System.out.println(
"Poll last entry of the ConcurrentSkipListMap ");
Map.Entry ple
= cmp.pollLastEntry();
System.out.println("key = " + ple.getKey()
+ " value = " + ple.getValue());
System.out.println("The entry set of the map is ");
Iterator > itr;
Set > eSet = cmp.entrySet();
itr = eSet.iterator();
while (itr.hasNext()) {
System.out.println(itr.next() + " ");
}
System.out.println(
"The concurrentSkipListMap contains Key 4 :"
+ cmp.containsKey(4));
System.out.println(
"The concurrentSkipListMap contains Value 9 :"
+ cmp.containsValue(9));
System.out.println(
"The size of the concurrentSkipListMap is "
+ cmp.size());
cmp.clear();
}
}
输出:
The key set of the map is
1 2 3 4 5 6
The values of the concurrentSkipListMap is
Sahil Kashish Tarun Karan Shweta Aditya
Poll first entry of the ConcurrentSkipListMap
key = 1 value = Sahil
Poll last entry of the ConcurrentSkipListMap
key = 6 value = Aditya
The entry set of the Map is
2=Kashish
3=Tarun
4=Karan
5=Shweta
The concurrentSkipListMap contains Key 4 :true
The concurrentSkipListMap contains Value 9 :false
The size of the concurrentSkipListMap is 6