hashMap jdk1.8新增方法
方法名称 | 解释 | 参数 |
---|---|---|
compute | 尝试计算用于指定键和其当前映射的值的映射(或 null 如果没有当前映射)。 |
compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) |
computeIfAbsent | 如果指定的键尚未与值相关联(或映射到 null ),则尝试使用给定的映射函数计算其值,并将其输入到此映射中,除非 null 。 |
computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction) |
computeIfPresent | 如果指定的密钥的值存在且非空,则尝试计算给定密钥及其当前映射值的新映射。 | computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction) |
getOrDefault | 返回到指定键所映射的值,或 defaultValue 如果此映射包含该键的映射 |
getOrDefault(Object key, V defaultValue) |
merge | 如果指定的键尚未与值相关联或与null相关联,则将其与给定的非空值相关联。 | merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction) |
putIfAbsent | 如果指定的键尚未与某个值相关联(或映射到 null ),则将其与给定值相关联并返回 null ,否则返回当前值。 |
putIfAbsent(K key, V value) |
compute
compute根据value可以自行判断是否更改当前value值
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
Backpack a = new Backpack("大箱子",1);
Backpack b = new Backpack("大箱子",12);
Backpack c = new Backpack("小箱子",13);
Backpack d = new Backpack("中箱子",14);
Backpack e = new Backpack("中箱子",11);
Backpack f = new Backpack("小箱子",12);
List<Backpack> backpacks = Arrays.asList(a, b, c, d, e, f);
//compute根据value可以自行判断是否更改当前value值
//lambda写法
backpacks.forEach(backpack->map.compute(backpack.getName(),(k,v)->v==null?backpack.getNum():v+backpack.getNum()));
//普通写法
for (int i = 0; i < backpacks.size(); i++) {
Backpack backpack = backpacks.get(i);
BiFunction<String, Integer, Integer> bif =new BiFunction<String, Integer, Integer>() {
@Override
public Integer apply(String key, Integer value) {
if(value==null){
return backpack.getNum();
}else{
return value+backpack.getNum();
}
}
};
map.compute(backpack.getName(),bif);
}
System.out.println(map);
}
//输出:{小箱子=25, 中箱子=25, 大箱子=13}
computeIfAbsent
computeIfAbsent根据key值进行判断,如果有值将不进行修改,没有值进行赋值
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
Backpack a = new Backpack("大箱子",1);
Backpack b = new Backpack("大箱子",12);
Backpack c = new Backpack("小箱子",13);
Backpack d = new Backpack("中箱子",14);
Backpack e = new Backpack("中箱子",11);
Backpack f = new Backpack("小箱子",12);
List<Backpack> backpacks = Arrays.asList(a, b, c, d, e, f);
//computeIfAbsent根据key值进行判断,如果有值将不进行修改,没有值进行赋值
//lambda写法
backpacks.forEach(backpack->map.computeIfAbsent(backpack.getName(),v->backpack.getNum()));
//普通写法
for (int i = 0; i <backpacks.size() ; i++) {
Backpack backpack = backpacks.get(i);
Function<String, Integer> function = new Function<String, Integer>() {
@Override
public Integer apply(String key) {
return backpack.getNum();
}
};
map.computeIfAbsent(backpack.getName(), function);
}
System.out.println(map);
}
//输出:{小箱子=13, 中箱子=14, 大箱子=1}
computeIfPresent
computeIfPresent 根据key值进行判断,如果没有值不进行赋值,如果有值并且非null,将自行判断是否更改当前值
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
Backpack a = new Backpack("大箱子",1);
Backpack b = new Backpack("大箱子",12);
Backpack c = new Backpack("小箱子",13);
Backpack d = new Backpack("中箱子",14);
Backpack e = new Backpack("中箱子",11);
Backpack f = new Backpack("小箱子",12);
List<Backpack> backpacks = Arrays.asList(a, b, c, d, e, f);
//computeIfPresent 根据key值进行判断,如果没有值不进行赋值,如果有值并且非null,将自行判断是否更改当前值
//lambda写法
backpacks.forEach(backpack->map.computeIfPresent(backpack.getName(),(k,v)->v==null?backpack.getNum():v+backpack.getNum()));
//普通写法
for (int i = 0; i < backpacks.size(); i++) {
Backpack backpack = backpacks.get(i);
BiFunction<String, Integer, Integer> bif =new BiFunction<String, Integer, Integer>() {
@Override
public Integer apply(String key, Integer value) {
if(value==null){
return backpack.getNum();
}else{
return value+backpack.getNum();
}
}
};
map.computeIfPresent(backpack.getName(),bif);
}
System.out.println(map);
}
//输出:{}
getOrDefault
getOrDefault获取指定key,如果不存在,返回指定默认value
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
//map.put("超级大箱子", 2);
//getOrDefault获取指定key,如果不存在,返回指定默认value
Integer num = map.getOrDefault("超级大箱子", 34);
map.put("超级大箱子", num);
System.out.println(map);
}
//key存在时输出:{超级大箱子=2}
//key不存在时输出:{超级大箱子=34}
merge
merge 根据key进行判断,如果存在将不进行操作,如果不存在,可以自行判断新旧值处理
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
Integer num = map.getOrDefault("超级大箱子", 34);
map.put("超级大箱子", num);
//merge 根据key进行判断,如果存在将不进行操作,如果不存在,可以自行判断新旧值处理
//lambda写法
map.merge("超级大箱子",3,(oldValue,newValue) -> oldValue+newValue);
//普通写法
BiFunction<Integer, Integer, Integer> biFunction =new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer integer, Integer integer2) {
System.out.println(integer);
System.out.println(integer2);
return integer + integer2;
}
};
map.merge("超级大箱子", 3, biFunction);
System.out.println(map);
}
//key不存在时输出:{超级大箱子=3}
//key存在时输出:{超级大箱子=37}
putIfAbsent
putIfAbsent 根据当前key进行判断,如果存在不进行更改,如果不存在将进行赋值
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
//map.put("小盒子",122);
//putIfAbsent 根据当前key进行判断,如果存在不进行更改,如果不存在将进行赋值
map.putIfAbsent("小盒子",12);
System.out.println(map);
}
//key存在时输出:{小盒子=122}
//key不存在时输出:
class Backpack{
private String name;//名称
private int num;//数量
public Backpack(String name,int num){
this.name=name;
this.num=num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}