package com.geo.map;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
public class ConcurrentSkipListMapTest {
public static void main(String[] args) {
ConcurrentSkipListMap<String,Integer> cslMap = new ConcurrentSkipListMap<String,Integer>();
cslMap.put("2017-05-22 16:18:10_key1", 1);
cslMap.put("2017-05-22 16:18:08_key2", 2);
cslMap.put("2017-05-22 16:18:20_key3", 1);
cslMap.put("2017-05-22 16:18:18_key4", 2);
cslMap.put("2017-05-22 16:18:30_key5", 1);
cslMap.put("2017-05-22 16:18:28_key2", 2);
cslMap.put("2017-05-22 16:18:40_key2", 1);
cslMap.put("2017-05-22 16:18:38_key1", 2);
cslMap.put("2017-05-22 16:18:59_key1", 2);
cslMap.put("2017-05-22 17:18:10_key1", 2);
cslMap.put("2017-05-22 17:18:08_key1", 2);
cslMap.put("2017-05-23 17:18:08_key1", 2);
String startKey = "2017-05-22 16:18:08";
String endKey = "2017-05-22 16:18:60";
ConcurrentNavigableMap<String, Integer> subMap = cslMap.subMap(startKey, endKey); //前閉后開
for (Entry<String, Integer> entry : subMap.entrySet()) { //取一定范圍的集合
System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
}
System.out.println("----------------------------------------------");
String firstKey = cslMap.firstKey(); //第一個鍵值(最小)
String lastKey = cslMap.lastKey();//最后一個鍵值(最大)
System.out.println("firstKey:" + firstKey + "=" + cslMap.get(firstKey) + " lastKey:" + lastKey + "=" + cslMap.get(lastKey));
System.out.println("----------------------------------------------");
ConcurrentNavigableMap<String, Integer> headMap = cslMap.headMap("2017-05-23");//截止到指定key的集合(toKey 開區間)
// ConcurrentNavigableMap<String, Integer> headMap = cslMap.headMap("2017-05-22",true);//截止到指定key的集合(toKey 閉區間)
for (Entry<String, Integer> entry : headMap.entrySet()) {
System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
}
System.out.println("----------------------------------------------");
/*Iterator<Entry<String, Integer>> it = cslMap.entrySet().iterator();
Set<String> deleteKeySet = headMap.keySet(); //要刪除的key
while (it.hasNext()) {
Entry<String, Integer> entry = it.next();
if (deleteKeySet.contains(entry.getKey()))
it.remove();
}*/
for (String key : headMap.keySet()) {
cslMap.remove(key);
}
for (Entry<String, Integer> entry : cslMap.entrySet()) { //刪除后的集合
System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
}
}
}
ps:
// 構造一個新的空映射,該映射按照鍵的自然順序進行排序。
ConcurrentSkipListMap()
// 構造一個新的空映射,該映射按照指定的比較器進行排序。
ConcurrentSkipListMap(Comparator<? super K> comparator)
// 構造一個新映射,該映射所包含的映射關系與給定映射包含的映射關系相同,並按照鍵的自然順序進行排序。
ConcurrentSkipListMap(Map<? extends K,? extends V> m)
// 構造一個新映射,該映射所包含的映射關系與指定的有序映射包含的映射關系相同,使用的順序也相同。
ConcurrentSkipListMap(SortedMap<K,? extends V> m)
// 返回與大於等於給定鍵的最小鍵關聯的鍵-值映射關系;如果不存在這樣的條目,則返回 null。
Map.Entry<K,V> ceilingEntry(K key)
// 返回大於等於給定鍵的最小鍵;如果不存在這樣的鍵,則返回 null。
K ceilingKey(K key)
// 從此映射中移除所有映射關系。
void clear()
// 返回此 ConcurrentSkipListMap 實例的淺表副本。
ConcurrentSkipListMap<K,V> clone()
// 返回對此映射中的鍵進行排序的比較器;如果此映射使用鍵的自然順序,則返回 null。
Comparator<? super K> comparator()
// 如果此映射包含指定鍵的映射關系,則返回 true。
boolean containsKey(Object key)
// 如果此映射為指定值映射一個或多個鍵,則返回 true。
boolean containsValue(Object value)
// 返回此映射中所包含鍵的逆序 NavigableSet 視圖。
NavigableSet<K> descendingKeySet()
// 返回此映射中所包含映射關系的逆序視圖。
ConcurrentNavigableMap<K,V> descendingMap()
// 返回此映射中所包含的映射關系的 Set 視圖。
Set<Map.Entry<K,V>> entrySet()
// 比較指定對象與此映射的相等性。
boolean equals(Object o)
// 返回與此映射中的最小鍵關聯的鍵-值映射關系;如果該映射為空,則返回 null。
Map.Entry<K,V> firstEntry()
// 返回此映射中當前第一個(最低)鍵。
K firstKey()
// 返回與小於等於給定鍵的最大鍵關聯的鍵-值映射關系;如果不存在這樣的鍵,則返回 null。
Map.Entry<K,V> floorEntry(K key)
// 返回小於等於給定鍵的最大鍵;如果不存在這樣的鍵,則返回 null。
K floorKey(K key)
// 返回指定鍵所映射到的值;如果此映射不包含該鍵的映射關系,則返回 null。
V get(Object key)
// 返回此映射的部分視圖,其鍵值嚴格小於 toKey。
ConcurrentNavigableMap<K,V> headMap(K toKey)
// 返回此映射的部分視圖,其鍵小於(或等於,如果 inclusive 為 true)toKey。
ConcurrentNavigableMap<K,V> headMap(K toKey, boolean inclusive)
// 返回與嚴格大於給定鍵的最小鍵關聯的鍵-值映射關系;如果不存在這樣的鍵,則返回 null。
Map.Entry<K,V> higherEntry(K key)
// 返回嚴格大於給定鍵的最小鍵;如果不存在這樣的鍵,則返回 null。
K higherKey(K key)
// 如果此映射未包含鍵-值映射關系,則返回 true。
boolean isEmpty()
// 返回此映射中所包含鍵的 NavigableSet 視圖。
NavigableSet<K> keySet()
// 返回與此映射中的最大鍵關聯的鍵-值映射關系;如果該映射為空,則返回 null。
Map.Entry<K,V> lastEntry()
// 返回映射中當前最后一個(最高)鍵。
K lastKey()
// 返回與嚴格小於給定鍵的最大鍵關聯的鍵-值映射關系;如果不存在這樣的鍵,則返回 null。
Map.Entry<K,V> lowerEntry(K key)
// 返回嚴格小於給定鍵的最大鍵;如果不存在這樣的鍵,則返回 null。
K lowerKey(K key)
// 返回此映射中所包含鍵的 NavigableSet 視圖。
NavigableSet<K> navigableKeySet()
// 移除並返回與此映射中的最小鍵關聯的鍵-值映射關系;如果該映射為空,則返回 null。
Map.Entry<K,V> pollFirstEntry()
// 移除並返回與此映射中的最大鍵關聯的鍵-值映射關系;如果該映射為空,則返回 null。
Map.Entry<K,V> pollLastEntry()
// 將指定值與此映射中的指定鍵關聯。
V put(K key, V value)
// 如果指定鍵已經不再與某個值相關聯,則將它與給定值關聯。
V putIfAbsent(K key, V value)
// 從此映射中移除指定鍵的映射關系(如果存在)。
V remove(Object key)
// 只有目前將鍵的條目映射到給定值時,才移除該鍵的條目。
boolean remove(Object key, Object value)
// 只有目前將鍵的條目映射到某一值時,才替換該鍵的條目。
V replace(K key, V value)
// 只有目前將鍵的條目映射到給定值時,才替換該鍵的條目。
boolean replace(K key, V oldValue, V newValue)
// 返回此映射中的鍵-值映射關系數。
int size()
// 返回此映射的部分視圖,其鍵的范圍從 fromKey 到 toKey。
ConcurrentNavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
// 返回此映射的部分視圖,其鍵值的范圍從 fromKey(包括)到 toKey(不包括)。
ConcurrentNavigableMap<K,V> subMap(K fromKey, K toKey)
// 返回此映射的部分視圖,其鍵大於等於 fromKey。
ConcurrentNavigableMap<K,V> tailMap(K fromKey)
// 返回此映射的部分視圖,其鍵大於(或等於,如果 inclusive 為 true)fromKey。
ConcurrentNavigableMap<K,V> tailMap(K fromKey, boolean inclusive)
// 返回此映射中所包含值的 Collection 視圖。
Collection<V> values()