【JAVA】哈希表HashMap中java8新增功能概述

getOrDefault

  • getOrDefault(Object key, V defaultValue)

  • 如果存在key返回对应的value,否则返回defaultValue

replaceAll

  • replaceAll(BiFunction<? super K, ? super V, ? extends V> function)

  • 以key为维度替换所有的value,替换逻辑在function中实现

putIfAbsent

  • putIfAbsent(K key, V value)    

  • 如果key存在则返回对应的value,否则将key和value添加到map中

computeIfAbsent

  • computeIfAbsent(K key,Function<? super K, ? extends V> mappingFunction)  

  • 如果key存在返回对应的value,如果不存在则通过function计算出value并放入map中

computeIfPresent

  • computeIfPresent(K key,BiFunction<? super K, ? super V, ? extends V> remappingFunction) 

  • 如果key存在则根据function计算一个新的value并新的value放入map,如果新value为null则从map中移除该key。

compute

  • compute(K key,BiFunction<? super K, ? super V, ? extends V> remappingFunction)  

  • 通过function计算key对应的newValue,newValue不为空则放入map,否则在key存在的情况下移除该key。

merge

  • merge(K key, V value,BiFunction<? super V, ? super V, ? extends V> remappingFunction)  

  • 基于key对应的oldValue和value通过function计算新的newValue,newValue不为空则放入map,否则从map中移除key。

END

猜你喜欢

转载自www.cnblogs.com/anliux/p/12364653.html
今日推荐