|
| 1 | +Java 8之Map新增方法 |
| 2 | + |
| 3 | +# getOrDefault |
| 4 | + |
| 5 | +``` |
| 6 | +map.getOrDefault("one", "s"); |
| 7 | +``` |
| 8 | + |
| 9 | +# forEach |
| 10 | + |
| 11 | +``` |
| 12 | +map.forEach(new BiConsumer<String, String>() { |
| 13 | + @Override |
| 14 | + public void accept(String key, String value) { |
| 15 | + System.out.println(key + ", " + value); |
| 16 | + } |
| 17 | +}); |
| 18 | +``` |
| 19 | + |
| 20 | +# replace(K key, V newValue) |
| 21 | + |
| 22 | +类似 |
| 23 | + |
| 24 | +``` |
| 25 | +if (map.containsKey(key)) { |
| 26 | + return map.put(key, newValue); |
| 27 | +} else { |
| 28 | + return null; |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | + |
| 33 | +# replace(K key, V oldValue, V newValue); |
| 34 | + |
| 35 | +如果key和oldValue都匹配,则替换成newValue。类似: |
| 36 | + |
| 37 | +``` |
| 38 | +if (map.containsKey(key) && Objects.equals(map.get(key), oldValue)) { |
| 39 | + map.put(key, newValue); |
| 40 | + return true; |
| 41 | +} else { |
| 42 | + return false; |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +# replaceAll |
| 47 | +替换Map中所有Entry的value值,这个值由旧的key和value计算得出 |
| 48 | + |
| 49 | +``` |
| 50 | +map.replaceAll(new BiFunction<String, String, String>() { |
| 51 | + @Override |
| 52 | + public String apply(String key, String value) { |
| 53 | + if (key.equals("two")) { |
| 54 | + return value + ".old"; |
| 55 | + } |
| 56 | + return value; |
| 57 | + } |
| 58 | +}); |
| 59 | +``` |
| 60 | + |
| 61 | +类似如下代码: |
| 62 | + |
| 63 | +``` |
| 64 | +for (Entry entry : map.entrySet()) { |
| 65 | + entry.setValue(function.apply(entry.getKey(), entry.getValue())); |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +# putIfAbsent(K key, V value) |
| 70 | + |
| 71 | +如果key不存在,则put,并返回null。如果key存在,则什么也不做,返回key对应的value |
| 72 | + |
| 73 | +``` |
| 74 | +map.putIfAbsent("four", "charge"); |
| 75 | +``` |
| 76 | + |
| 77 | +# remove(K key, V value) |
| 78 | + |
| 79 | +``` |
| 80 | +map.remove(key, value); |
| 81 | +``` |
| 82 | + |
| 83 | +类似: |
| 84 | + |
| 85 | +``` |
| 86 | +if (map.containsKey(key) && Objects.equals(map.get(key), value)) { |
| 87 | + map.remove(key); |
| 88 | + return true; |
| 89 | +} else { |
| 90 | + return false; |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +# computeIfAbsent(K key, Function f) |
| 95 | + |
| 96 | +``` |
| 97 | +String s = map.computeIfAbsent("four", new Function<String, String>() { |
| 98 | + @Override |
| 99 | + public String apply(String key) { |
| 100 | + return key + "@"; |
| 101 | + } |
| 102 | +}); |
| 103 | +``` |
| 104 | + |
| 105 | +类似 |
| 106 | + |
| 107 | +``` |
| 108 | +if (map.get(key) == null) { |
| 109 | + V newValue = function.apply(key); |
| 110 | + if (newValue != null) { |
| 111 | + map.put(key, newValue); |
| 112 | + return newValue; |
| 113 | + } |
| 114 | +} else { |
| 115 | + return map.get(key); |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +# computeIfPresent(K key, BiFunction f) |
| 120 | + |
| 121 | +``` |
| 122 | +map.computeIfPresent("one", new BiFunction<String, String, String>() { |
| 123 | + @Override |
| 124 | + public String apply(String key, String value) { |
| 125 | + return null; |
| 126 | + } |
| 127 | +}); |
| 128 | +``` |
| 129 | + |
| 130 | +类似: |
| 131 | + |
| 132 | +``` |
| 133 | +if (map.get(key) != null) { |
| 134 | + V oldValue = map.get(key); |
| 135 | + V newValue = function.apply(key, oldValue); |
| 136 | + if (newValue != null) { |
| 137 | + map.put(key, newValue); |
| 138 | + } else { |
| 139 | + map.remove(key); |
| 140 | + } |
| 141 | + return newValue; |
| 142 | +} else { |
| 143 | + return null; |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +# compute(K key, BiFunction f) |
| 148 | + |
| 149 | +``` |
| 150 | +map.compute(key, new BiFunction<String, String, String>() { |
| 151 | + @Override |
| 152 | + public String apply(String key, String value) { |
| 153 | + return key + "@" + value; |
| 154 | + } |
| 155 | +}); |
| 156 | +``` |
| 157 | + |
| 158 | +类似: |
| 159 | + |
| 160 | +``` |
| 161 | +V oldValue = map.get(key); |
| 162 | +V newValue = function.apply(key, value); |
| 163 | +
|
| 164 | +if (oldValue != null) { |
| 165 | + if (newValue != null) { |
| 166 | + map.put(key, newValue); |
| 167 | + } else { |
| 168 | + map.remove(key); |
| 169 | + } |
| 170 | +} else { |
| 171 | + if (newValue != null) { |
| 172 | + map.put(key, newValue); |
| 173 | + } |
| 174 | +} |
| 175 | +return newValue; |
| 176 | +``` |
| 177 | + |
| 178 | +# merge(K key, V value, BiFunction f) |
| 179 | + |
| 180 | +apply中第一个参数是oldValue,第二个参数是merge传入的第二个参数 |
| 181 | + |
| 182 | +``` |
| 183 | +map.merge(key, value, new BiFunction<String, String, String>() { |
| 184 | + @Override |
| 185 | + public String apply(String oldValue, String value) { |
| 186 | + return oldValue + "@" + value; |
| 187 | + } |
| 188 | +}); |
| 189 | +``` |
| 190 | + |
| 191 | +类似: |
| 192 | + |
| 193 | +``` |
| 194 | +if (value == null) { |
| 195 | + throw new NullPointerException(); |
| 196 | +} |
| 197 | +
|
| 198 | +V oldValue = map.get(key); |
| 199 | +V newValue = (oldValue == null) ? value : function.apply(oldValue, value); |
| 200 | +
|
| 201 | +if (newValue == null) { |
| 202 | + map.remove(key); |
| 203 | +} else { |
| 204 | + map.put(key, newValue); |
| 205 | + return newValue; |
| 206 | +} |
| 207 | +``` |
0 commit comments