Skip to content

ConcurrentHashMap 使用示例

Published: at 10:57:12

构造方法

// 1.无参数构造方法
new ConcurrentHashMap();

// 2.指定初始容量
new ConcurrentHashMap(initialCapacity)

// 3.指定初始容量和加载因子
new ConcurrentHashMap(initialCapacity,loadFactor)

// 4.指定初始容量和加载因子与并发级别(并发更新线程数)
new ConcurrentHashMap(initialCapacity, loadFactor, concurrencyLevel)

// 5.创建与给定映射具有相同映射的新映射
new ConcurrentHashMap(Map<? extends K, ? extends V> m)

方法介绍

新增元素

// 1.添加元素,不允许null
map.put(1,1);

// 2.添加一个map
map.putAll(map);
// 3.添加元素, 键不存在映射关系才添加成功
map.putIfAbsent(2,1);

删除元素

// 1.移除指定的key
map.remove(1);
// 2.移除指定的key,value
map.remove(2,2);

替换元素

// 1.替换指定key
map.replace(1,3);

// 2.替换指定key,value
map.replace(1,2,4);

// 3.替换所有,这里是把value都乘以2
map.replaceAll((key,value)->value << 1);

查找元素

// 1.返回到指定键所映射的值
map.get("精灵王");

// 2. 返回指定键映射到的值,如果此映射不包含该键的映射,则返回给定的默认值。
map.getOrDefault("精灵王","jingling.im");

// 指定搜素函数
// 1.通过在每个(键,值)上应用给定的搜索函数返回非空结果,如果没有则返回null。
//   成功后,进一步的元素处理被抑制,并且搜索功能的任何其他并行调用的结果被忽略。
Object a = map.search(5,(key,value)->{
    return (value & 1) == 0 ? value : null; // 查找第一个偶数,如果是奇数返回null
});

// 2. 每个键上应用给定搜索函数的非null结果,如果没有则返回null。
String k = map.searchKeys(5,(key)->{
    return "jinglingwang".equals(key) ? key : null;
});
System.out.println(k);

// 3. 对每个值应用给定的搜索函数,如果没有,返回null。
Integer v2 = map.searchValues(5,(value)->{
    return value == 2 ? 520 : null;
});
System.out.println(v2);

// 4.从每个entry应用给定的搜索函数,如果没有,则返回null。
Integer a = map.searchEntries(5,(entry)->{
    System.out.println("entry:"+entry.getKey() + "   " + entry.getValue());
    return "jingling.im".equals(entry.getKey()) ? 1 : null;
});
System.out.println(a);

包含

// 1. 验证值是否存在, 等同于map.containsValue()方法
map.contains(4)

// 2. 验证key是否存在
map.containsKey("精灵王")

// 3. 验证值是否存在,等同于map.contains()方法
map.containsValue(4)

遍历

// 1.返回此表中键的枚举
map.keys();

// 2.返回此地图中包含的键的Set视图
map.keySet();

// 3.返回此地图中键的Set视图,使用给定的映射值
map.keySet(100)

// 4. 返回此表中值的枚举。
map.elements();

// 5.返回此地图中包含的映射的Set视图。
map.entrySet().forEach((entry)->{
    System.out.println(entry.getKey() + " " + entry.getValue());
});

// 6.1 同时遍历key,value
map.forEach((key,value)->{
    System.out.println(key+ " " + value);
});
// 6.2 同时遍历key,value,找到满足条件的执行响应函数
map.forEach(5,(key,value)->{
    System.out.println(key+ " " + value);
    return (value & 1) == 0 ? "jingling.im" : null;
},aa->{
    System.out.println(aa); // 条件中有偶数,每次就输出jingling.im
});

// 7.1遍历
map.forEachEntry(5,(entry)->{
    System.out.println(entry.getKey() + " " + entry.getValue());
});

// 7.2遍历查找,满足条件执行响应的函数
map.forEachEntry(5,(entry)->{
    System.out.println(entry.getKey() + " " + entry.getValue());
    return (value & 1) == 0 ? "jingling.im" : null;
},aa->{
    System.out.println(aa); // 条件中有偶数,每次就输出jingling.im
});

// 8.1遍历key
map.forEachKey(5,key->{
    System.out.println("key: " + key);
});
// 8.2遍历key,满足条件的key执行响应的函数
map.forEachKey(5,key->{
    System.out.println("key: " + key);
    return !key.equals("jinglingwang") ? "jingling.im" : null;
},kk ->{
    System.out.println(kk);
});

// 9.1 遍历value
map.forEachValue(5,value->{
    System.out.println("value: " + value);
});
// 9.2 遍历value,满足条件的value执行响应的函数
map.forEachValue(5,value->{
    System.out.println("value: " + value);
    return (value & 1) == 0 ? "jingling.im" : null;
},vv ->{
    System.out.println(vv);
});

reduce归约

reduce()

//新增元素
map.put("1",0);
map.put("2",4);
map.put("3",6);
map.put("4",8);
// 1. 返回使用给定的reducer组合值来累积所有(键,值)对的给定转换的结果,如果没有,则返回null
String res =  map.reduce(5,(key,value)->{
    System.out.println("transformer key:"+key+" value:"+value);
    return value+"";
},(key,value)->{
    System.out.println("reducer key:"+key+" value:"+value);
    return key+value+"";
});
System.out.println("res:"+res);

以上示例输出如下:

transformer key:1 value:0
transformer key:2 value:4
reducer key:0 value:4
transformer key:3 value:6
reducer key:04 value:6
transformer key:4 value:8
reducer key:046 value:8
res:0468

第一个transformer函数遍历整个map,然后将返回值传递到reduce函数进行计算,之后reduce函数的返回值再次参与到下一次的reduce函数的计算中。

reduceToDouble()

double reduceToDouble = map.reduceToDouble(5,(key,value)->{
    System.out.println("transformer key:"+key);
    return value;
},100,(key,value)->{
    System.out.println("reducer key:"+key+" value:"+value);
    return key+value;
});
System.out.println("reduceToDouble:"+reduceToDouble);
// 输出结果
transformer key:1 value:0
reducer key:100.0 value:0.0
transformer key:2 value:4
reducer key:100.0 value:4.0
transformer key:3 value:6
reducer key:104.0 value:6.0
transformer key:4 value:8
reducer key:110.0 value:8.0
reduceToDouble:118.0

遍历归约所有的键值对,并把结果转换成double类型。

类似的方法还有reduceToInt()reduceToLong()

reduceEntries()

// 第一种,返回值为entry
Map.Entry reduceEntries =  map.reduceEntries(5,entry->{
    System.out.println("transformer key:"+entry.getKey()+" value:"+entry.getValue());
    return entry;
},(key,value)->{
    System.out.println("reducer key:"+key+" value:"+value);
    return value;
});
System.out.println("reduceEntries:"+reduceEntries);
// 输出如下
transformer key:1 value:0
transformer key:2 value:4
reducer key:1=0 value:2=4
transformer key:3 value:6
reducer key:2=4 value:3=6
transformer key:4 value:8
reducer key:3=6 value:4=8
reduceEntries:4=8

// 第二种
String reduceEntries2 =  map.reduceEntries(5,(entry)->{
    System.out.println("transformer key:"+entry.getKey()+" value:"+entry.getValue());
    return entry.getValue()+"";
},(key,value)->{
    System.out.println("reducer key:"+key+" value:"+value);
    return key+value+"";
});
System.out.println("reduceEntries2:"+reduceEntries2);
transformer key:1 value:0
transformer key:2 value:4
reducer key:0 value:4
transformer key:3 value:6
reducer key:04 value:6
transformer key:4 value:8
reducer key:046 value:8
reduceEntries2:0468

transformer 函数每次返回的是一个entry,然后会传入到reduce函数中,reduce的return值会继续传递到下一次的reduce计算。

reduceEntriesToDouble()

// 返回使用给定的reducer累加给定变换的结果,以组合值,给定基础作为一个标识值
Double reduceEntriesToDouble = map.reduceEntriesToDouble(5,entry->{
    System.out.println("transformer key:"+entry.getKey()+" value:"+entry.getValue());
    return Double.valueOf(entry.getValue());
},100,(l,r)->{
    System.out.println("reducer key:"+l+" value:"+r);
    return l+r;
});
System.out.println("reduceEntriesToDouble:"+reduceEntriesToDouble);
// 输出结果
transformer key:1 value:0
reducer key:100.0 value:0.0
transformer key:2 value:4
reducer key:100.0 value:4.0
transformer key:3 value:6
reducer key:104.0 value:6.0
transformer key:4 value:8
reducer key:110.0 value:8.0
reduceEntriesToDouble:118.0

其中transformer函数是以entry的形式来进行遍历的,第三个参数是一个归约的标识(初始默认值),功能和reduceEntriesToInt()reduceEntriesToLong()两个方法非常相似。

reduceKeys()

// 1.第一种,只有reduce函数,遍历所有的键值对,reduce函数的返回值会作为下一次的key,如果没有则返回null。
String newKey = map.reduceKeys(5,(key,value)->{
    System.out.println("reducer key:"+key+" value:"+value);
    return key+value;
**}**);
System.out.println("newKey:"+newKey);
// 输出结果
reducer key:1 value:2
reducer key:12 value:3
reducer key:123 value:4
newKey:1234

// 2.第二种,有transformer和reducer
String newValue = map.reduceKeys(5,(key)->{
    System.out.println("transformer key:"+key);
    return key;
},(key,value)->{
    System.out.println("reducer key:"+key+" value:"+value);
    return key+value;
});
System.out.println("newValue:"+newValue);
// 输出结果
transformer key:1
transformer key:2
reducer key:1 value:2
transformer key:3
reducer key:12 value:3
transformer key:4
reducer key:123 value:4
newValue:1234

reduceKeysToDouble()

double reduceKeysToDouble = map.reduceKeysToDouble(5,(key)->{
    System.out.println("transformer key:"+key);
    return Double.parseDouble(key);
},100,(key,value)->{
    System.out.println("reducer key:"+key+" value:"+value);
    return key+value;
});
System.out.println("reduceKeysToDouble:"+reduceKeysToDouble);

遍历归约所有的key,并把结果转换成double类型。

类似的方法还有reduceKeysToInt()reduceKeysToLong()

reduceValues()

// 第一种:仅reducer函数
double reduceValues = map.reduceValues(5,(key,value)->{
    System.out.println("reducer key:"+key+" value:"+value);
    return key+value;
});
System.out.println("reduceValues:"+reduceValues);
//输出结果
reducer key:0 value:4
reducer key:4 value:6
reducer key:10 value:8
reduceValues:18.0

// 第二种,含有transformer
double reduceValues2 = map.reduceValues(5,(value)->{
    System.out.println("transformer value:"+value);
    return Double.valueOf(value);
},(key,value)->{
    System.out.println("reducer key:"+key+" value:"+value);
    return key+value;
});
System.out.println("reduceValues2:"+reduceValues2);
// 输出结果
transformer value:0
transformer value:4
reducer key:0.0 value:4.0
transformer value:6
reducer key:4.0 value:6.0
transformer value:8
reducer key:10.0 value:8.0
reduceValues2:18.0

reduceValuesToDouble()

double reduceValuesToDouble = map.reduceValuesToDouble(5,(value)->{
    System.out.println("transformer value:"+value);
    return Double.valueOf(value);
},100,(l,r)->{
    System.out.println("reducer key:"+l+" value:"+r);
    return l+r;
});
System.out.println("reduceValuesToDouble:"+reduceValuesToDouble);
// 输出结果
transformer value:0
reducer key:100.0 value:0.0
transformer value:4
reducer key:100.0 value:4.0
transformer value:6
reducer key:104.0 value:6.0
transformer value:8
reducer key:110.0 value:8.0
reduceValuesToDouble:118.0

遍历归约所有的value,并把结果转换成double类型。

类似的方法还有reduceValuesToInt()reduceValuesToLong()