|
| 1 | +import java.util.Collection; |
| 2 | +import java.util.Comparator; |
| 3 | +import java.util.Map; |
| 4 | +import java.util.NavigableMap; |
| 5 | +import java.util.NavigableSet; |
| 6 | +import java.util.Set; |
| 7 | +import java.util.Map.Entry; |
| 8 | +import java.util.concurrent.ConcurrentSkipListMap; |
| 9 | +public class ConcurrentSkipListMapMethods { |
| 10 | + public static void main(String[] args) throws Exception { |
| 11 | + //Put |
| 12 | + ConcurrentSkipListMap<String, String> map = new ConcurrentSkipListMap<>(); |
| 13 | + map.put("1", "one"); |
| 14 | + map.put("2", "two"); |
| 15 | + map.put("3", "three"); |
| 16 | + map.put("4", "four"); |
| 17 | + map.put("5", "five"); |
| 18 | + map.put("6", "six"); |
| 19 | + map.put("7", "seven"); |
| 20 | + |
| 21 | + |
| 22 | + System.out.println("Map:" + map); |
| 23 | + System.out.println(" "); |
| 24 | + |
| 25 | + //ceilingEntry(K key) |
| 26 | + Map.Entry<String, String> entry = map.ceilingEntry("3"); |
| 27 | + System.out.println("ceilingEntry:" + entry); |
| 28 | + Entry<String, String> entry1 = map.ceilingEntry("5"); |
| 29 | + System.out.println("ceilingEntry:" + entry1); |
| 30 | + |
| 31 | + //ceilingKey(K key) |
| 32 | + String key = map.ceilingKey("3"); |
| 33 | + System.out.println("ceilingKey:" + key); |
| 34 | + String key1 = map.ceilingKey("2"); |
| 35 | + System.out.println("ceilingKey:" + key1); |
| 36 | + |
| 37 | + // clone() |
| 38 | + ConcurrentSkipListMap<String, String> map1 = (ConcurrentSkipListMap<String, String>) map.clone(); |
| 39 | + System.out.println("clone:" + map1); |
| 40 | + |
| 41 | + // compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) |
| 42 | + |
| 43 | + map1.compute("1", (k, v) -> v.toUpperCase()); |
| 44 | + System.out.println("compute:" + map1); |
| 45 | + |
| 46 | + // computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction) |
| 47 | + map1.computeIfAbsent("0", k->"Zero".toUpperCase()); |
| 48 | + System.out.println("compute:" + map1); |
| 49 | + |
| 50 | + // computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) |
| 51 | + map1.computeIfPresent("0", (k, v) -> v.toLowerCase()); |
| 52 | + System.out.println("compute:" + map1); |
| 53 | + |
| 54 | + |
| 55 | + // containsKey(Object key) |
| 56 | + boolean b = map1.containsKey("0"); |
| 57 | + System.out.println("containsKey:" + b); |
| 58 | + |
| 59 | + // containsValue(Object value) |
| 60 | + boolean b1 = map1.containsValue("zero"); |
| 61 | + System.out.println("containsValue:" + b1); |
| 62 | + |
| 63 | + // entrySet() |
| 64 | + Set<Entry<String, String>> set = map1.entrySet(); |
| 65 | + System.out.println("entrySet:" + set); |
| 66 | + |
| 67 | + // equals(Object o) |
| 68 | + boolean b2 = map1.equals(map); |
| 69 | + System.out.println("Equals:" + b2); |
| 70 | + |
| 71 | + // firstEntry() |
| 72 | + Entry<String, String> entry2 = map1.firstEntry(); |
| 73 | + System.out.println("firstEntry:" + entry2); |
| 74 | + |
| 75 | + //firstKey() |
| 76 | + String key2 = map1.firstKey(); |
| 77 | + System.out.println("firstKey:" + key2); |
| 78 | + |
| 79 | + //floorEntry(K key) |
| 80 | + Entry<String, String> entry3 = map1.floorEntry("3"); |
| 81 | + System.out.println("floorEntry:" + entry3); |
| 82 | + |
| 83 | + //floorKey(K key) |
| 84 | + String key3 = map1.floorKey("3"); |
| 85 | + System.out.println("floorKey:" + key3); |
| 86 | + |
| 87 | + // get(Object key) |
| 88 | + String value = map1.get("3"); |
| 89 | + System.out.println("get:" + value); |
| 90 | + |
| 91 | + //getOrDefault(Object key, V defaultValue) |
| 92 | + String value1 = map1.getOrDefault("3", "three"); |
| 93 | + System.out.println("getOrDefault:" + value1); |
| 94 | + |
| 95 | + //headMap(K toKey) |
| 96 | + Map<String, String> map2 = map1.headMap("3"); |
| 97 | + System.out.println("headMap:" + map2); |
| 98 | + |
| 99 | + //headMap(K toKey, boolean inclusive) |
| 100 | + Map<String, String> map3 = map1.headMap("3", true); |
| 101 | + System.out.println("headMap:" + map3); |
| 102 | + Map<String, String> map4 = map1.headMap("3", false); |
| 103 | + System.out.println("headMap:" + map4); |
| 104 | + |
| 105 | + // higherEntry(K key) |
| 106 | + Entry<String, String> entry4 = map1.higherEntry("3"); |
| 107 | + System.out.println("higherEntry:" + entry4); |
| 108 | + |
| 109 | + // higherKey(K key) |
| 110 | + String key4 = map1.higherKey("3"); |
| 111 | + System.out.println("higherKey:" + key4); |
| 112 | + |
| 113 | + // keySet() |
| 114 | + Set<String> set1 = map1.keySet(); |
| 115 | + System.out.println("keySet:" + set1); |
| 116 | + |
| 117 | + // lastEntry() |
| 118 | + Entry<String, String> entry5 = map1.lastEntry(); |
| 119 | + System.out.println("lastEntry:" + entry5); |
| 120 | + |
| 121 | + // lastKey() |
| 122 | + String key5 = map1.lastKey(); |
| 123 | + System.out.println("lastKey:" + key5); |
| 124 | + |
| 125 | + // lowerEntry(K key) |
| 126 | + Entry<String, String> entry6 = map1.lowerEntry("3"); |
| 127 | + System.out.println("lowerEntry:" + entry6); |
| 128 | + |
| 129 | + //lowerKey(K key) |
| 130 | + String key6 = map1.lowerKey("3"); |
| 131 | + System.out.println("lowerKey:" + key6); |
| 132 | + |
| 133 | + //merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction) |
| 134 | + map1.merge("3", "three", (v1, v2) -> v1 + v2); |
| 135 | + System.out.println("merge:" + map1); |
| 136 | + |
| 137 | + // pollFirstEntry() |
| 138 | + Entry<String, String> entry7 = map1.pollFirstEntry(); |
| 139 | + System.out.println("pollFirstEntry:" + entry7); |
| 140 | + |
| 141 | + // pollLastEntry() |
| 142 | + |
| 143 | + Entry<String, String> entry8 = map1.pollLastEntry(); |
| 144 | + System.out.println("pollFirstEntry:" + entry8); |
| 145 | + |
| 146 | + |
| 147 | + // putIfAbsent(K key, V value) |
| 148 | + map1.putIfAbsent("8", "Eight"); |
| 149 | + System.out.println("putIfAbsent:" + map1); |
| 150 | + |
| 151 | + // remove(Object key) |
| 152 | + map1.remove("8"); |
| 153 | + System.out.println("remove:" + map1); |
| 154 | + |
| 155 | + //remove(Object key, Object value) |
| 156 | + |
| 157 | + map1.remove("3", "threethree"); |
| 158 | + System.out.println("remove:" + map1); |
| 159 | + |
| 160 | + //replace(K key, V value) |
| 161 | + map1.replace("2", "TWO"); |
| 162 | + System.out.println("replace:" + map1); |
| 163 | + |
| 164 | + //replace(K key, V oldValue, V newValue) |
| 165 | + map1.replace("2", "TWO", "two"); |
| 166 | + System.out.println("replace:" + map1); |
| 167 | + |
| 168 | + //subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) |
| 169 | + Map<String, String> map5 = map1.subMap("2", true, "4", true); |
| 170 | + System.out.println("subMap:" + map5); |
| 171 | + |
| 172 | + //subMap(K fromKey, K toKey) |
| 173 | + Map<String, String> map6 = map1.subMap("2", "4"); |
| 174 | + System.out.println("subMap:" + map6);// By Default toInclusive is False |
| 175 | + |
| 176 | + //tailMap(K fromKey) |
| 177 | + Map<String, String> map7 = map1.tailMap("2"); |
| 178 | + System.out.println("tailMap:" + map7); |
| 179 | + |
| 180 | + // tailMap(K fromKey, boolean inclusive) |
| 181 | + Map<String, String> map8 = map1.tailMap("2", true); |
| 182 | + System.out.println("tailMap:" + map8); |
| 183 | + |
| 184 | + //values() |
| 185 | + Collection<String> collection = map1.values(); |
| 186 | + System.out.println("values:" + collection); |
| 187 | + |
| 188 | + /* java.util.AbstractMap -- Methods*/ |
| 189 | + |
| 190 | + // hashCode() |
| 191 | + int hashCode = map1.hashCode(); |
| 192 | + System.out.println("hashCode:" + hashCode); |
| 193 | + |
| 194 | + // isEmpty() |
| 195 | + boolean b3 = map1.isEmpty(); |
| 196 | + System.out.println("isEmpty:" + b3); |
| 197 | + |
| 198 | + // putAll(Map<? extends K,? extends V> m) |
| 199 | + |
| 200 | + map1.putAll(map); |
| 201 | + System.out.println("putAll:" + map1); |
| 202 | + |
| 203 | + // size() |
| 204 | + int size = map1.size(); |
| 205 | + System.out.println("size:" + size); |
| 206 | + |
| 207 | + //toString() |
| 208 | + String str = map1.toString(); |
| 209 | + System.out.println("toString:" + str); |
| 210 | + |
| 211 | + /* java.util.concurrent.ConcurrentMap - Methods */ |
| 212 | + |
| 213 | + // forEach(BiConsumer<? super K,? super V> action) |
| 214 | + map1.forEach((k, v) -> System.out.println("Key:" + k + " Value:" + v)); |
| 215 | + |
| 216 | + // replaceAll(BiFunction<? super K,? super V,? extends V> function) |
| 217 | + |
| 218 | + map1.replaceAll((k, v) -> v + "1"); |
| 219 | + System.out.println("replaceAll:" + map1); |
| 220 | + |
| 221 | + |
| 222 | + /* java.util.concurrent.ConcurrentNavigableMap - Methods */ |
| 223 | + |
| 224 | + // descendingKeySet() |
| 225 | + NavigableSet<String> set2 = map.descendingKeySet(); |
| 226 | + System.out.println("descendingKeySet:" + set2); |
| 227 | + |
| 228 | + // descendingMap() |
| 229 | + NavigableMap<String, String> map9 = map.descendingMap(); |
| 230 | + System.out.println("descendingMap:" + map9); |
| 231 | + |
| 232 | + //navigableKeySet() |
| 233 | + NavigableSet<String> set3 = map.navigableKeySet(); |
| 234 | + System.out.println("navigableKeySet:" + set3); |
| 235 | + |
| 236 | + /* java.util.Map -- Methods */ |
| 237 | + |
| 238 | + // hashCode() |
| 239 | + int hashCode1 = map.hashCode(); |
| 240 | + System.out.println("hashCode:" + hashCode1); |
| 241 | + |
| 242 | + //putAll(Map<? extends K,? extends V> m) |
| 243 | + |
| 244 | + map1.putAll(map); |
| 245 | + System.out.println("putAll:" + map1); |
| 246 | + |
| 247 | + // size() |
| 248 | + |
| 249 | + int size1 = map1.size(); |
| 250 | + |
| 251 | + System.out.println("size:" + size1); |
| 252 | + |
| 253 | + // java.util.SortedMap -- Methods // |
| 254 | + // comparator() |
| 255 | + Comparator<? super String> comparator = map.comparator(); |
| 256 | + ConcurrentSkipListMap<String, String> map10 = new ConcurrentSkipListMap<>(comparator); |
| 257 | + |
| 258 | + map10.putAll(map); |
| 259 | + System.out.println("comparator:" + map10); |
| 260 | + |
| 261 | + ConcurrentSkipListMap<String, String> map11 = new ConcurrentSkipListMap<>(Comparator.reverseOrder()); |
| 262 | + |
| 263 | + map11.putAll(map); |
| 264 | + System.out.println("comparator:" + map11); |
| 265 | + |
| 266 | + |
| 267 | + |
| 268 | + } |
| 269 | + |
| 270 | +} |
0 commit comments