最近比较忙,花了三天时间学习ConcurrentHashMap api,该类是JUC原子包中的类,通过单元测试代码把所有public api方法跑了一遍,大致了解了底层实现,初学乍练,有很多一知半解的地方,待后续有了深入理解再来补充
package test.java.util.concurrent;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import org.junit.Test;
/**
* ConcurrentHashMap的测试类
*
* @date 2020-07-01 21:05:23
*/
public class ConcurrentHashMapTest {
/**
*无参构造函数
* @Param
*/
@Test
public void testConstruct0()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
System.out.println(map.get("3333"));
}
/**
* 按照容量初始化
* 需要计算
* @Param
*/
@Test
public void testConstruct1()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap(3);
System.out.println(map.get("3333"));
}
/**
* 根据已有map初始化
* @Param
*/
@Test
public void testConstruct2()throws Exception{
Map param=new ConcurrentHashMap<>();
param.put("1","1");
param.put("2","2");
param.put("3","3");
ConcurrentHashMap map=new ConcurrentHashMap(param);
System.out.println(map.get("3"));
}
/**
* 根据容量及负载因子初始化
* @Param
*/
@Test
public void testConstruct3()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap(3,34f);
System.out.println(map.size());
}
/**
*根据容量及负载因子和并发级别初始化初始化
* @Param
*/
@Test
public void testConstruct4()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap(3,34f,3);
System.out.println(map.size());
}
/**
* map中键值对数量
* @Param
*/
@Test
public void testSize()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","1");
map.put("2","2");
map.put("3","3");
System.out.println(map.size());
}
/**
*是否为空
* @Param
*/
@Test
public void testIsEmpty()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","1");
map.put("2","2");
map.put("3","3");
System.out.println(map.isEmpty());
}
/**
* 根据key获取value
* @Param
*/
@Test
public void testGet()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("123","1");
map.put("123","2");
map.put("3","3");
System.out.println(map.get("123"));
}
/**
*是否包含key
* @Param
*/
@Test
public void testContainsKey()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("123","1");
map.put("123","2");
map.put("3","3");
System.out.println(map.containsKey("123"));
}
/**
*是否包含value
* @Param
*/
@Test
public void testContainsValue()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("123","2");
map.put("3","3");
System.out.println(map.containsValue("31"));
}
/**
* 存放值onlyIfAbsent 如果当前位置已存在一个值,是否替换,false是替换,true是不替换
*
* @Param
*/
@Test
public void testPut()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("123","2");
map.put("3","3");
System.out.println(map.containsValue("31"));
}
/**
*将map中的键值对放入map1中
* @Param
*/
@Test
public void testPutAll()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("123","2");
map.put("3","3");
ConcurrentHashMap map1=new ConcurrentHashMap();
map1.putAll(map);
System.out.println(map1.get("1"));
System.out.println(map.get("1"));
}
/**
* 移除key为1的键值对
* @Param
*/
@Test
public void testRemove1()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("123","2");
map.put("3","3");
System.out.println(map.remove("1"));
System.out.println(map.get("1"));
}
/**
*清空map
* @Param
*/
@Test
public void testClear()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("123","2");
map.put("3","3");
map.clear();
System.out.println(map.get("1"));
}
/**
* 获取key的set集合
* @Param
*/
@Test
public void testKeySet1()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("123","2");
map.put("3","3");
ConcurrentHashMap.KeySetView keySetView=map.keySet();
Iterator iterator=keySetView.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println(keySetView.size());
}
/**
* 获取value集合
* @Param
*/
@Test
public void testValues()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("123","2");
map.put("3","3");
Collection collection=map.values();
Iterator iterator=collection.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
/**
* Map.Entry遍历所有键值对
* @Param
*/
@Test
public void testEntrySet()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("123","2");
map.put("3","3");
Set> entries=map.entrySet();
Iterator iterator=entries.iterator();
while (iterator.hasNext()){
Map.Entry entry= (Map.Entry) iterator.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
/**
*hashcode计算key+value的hashcode
* 再求和
* @Param
*/
@Test
public void testHashCode()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("123","2");
map.put("3","3");
System.out.println(map.hashCode());
}
/**
*{1=31, 123=2, 3=3}
*
* @Param
*/
@Test
public void testToString()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("123","2");
map.put("3","3");
System.out.println(map.toString());
}
/**
* 比较的是所有key和所有value
* @Param
*/
@Test
public void testEquals()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("123","2");
map.put("3","3");
ConcurrentHashMap map1=new ConcurrentHashMap();
map1.put("1","31");
map1.put("3","3");
map1.put("123","2");
System.out.println(map.equals(map));
System.out.println(map.equals(map1));
}
/**
*如果key对应的value已经存在则返回value不存放新value
* 如果key对应value不存在,则放入value,并返回null
* @Param
*/
@Test
public void testPutIfAbsent()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
System.out.println(map.putIfAbsent("1231","22"));
System.out.println(map.get("1231"));
System.out.println(map.get("123"));
}
/**
* 按照键值对删除元素
* @Param
*/
@Test
public void testRemove()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
map.remove("123","2w");
System.out.println(map.get("123"));
}
/**
* 替换掉key所对应的value值
* @Param
*/
@Test
public void testReplace1()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
map.replace("123","2","13");
System.out.println(map.get("123"));
}
/**
* 替换掉key所对应的值
* @Param
*/
@Test
public void testReplace()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
map.replace("123","22321");
System.out.println(map.get("123"));
}
/**
* 获取key对应的value值,如果没有则返回指定默认值
* @Param
*/
@Test
public void testGetOrDefault()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
System.out.println(map.getOrDefault("1233","13321"));
}
/**
* forEach:使用BIConsumer遍历map
* @Param
*/
@Test
public void testForEach2()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
map.forEach((key,val)-> System.out.println(key+":"+val));
}
/**
* 用BiFunction计算的结果替换掉所有map中的key所对应的value值
* @Param
*/
@Test
public void testReplaceAll()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
map.replaceAll((key,oldVal)-> key+":"+oldVal);
Set> entrySet= (Set>) map.entrySet();
Iterator iterator=entrySet.iterator();
while (iterator.hasNext()){
Map.Entry entry= (Map.Entry) iterator.next();
System.out.println(entry.getKey()+"-"+entry.getValue());
}
}
/**
* 如果key对应的value为空则将用Function计算得到的值存入
* @Param
*/
@Test
public void testComputeIfAbsent()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
map.computeIfAbsent("1231",(key)-> key+"113123");
System.out.println(map.get("1234"));
Set> entrySet= (Set>) map.entrySet();
Iterator iterator=entrySet.iterator();
while (iterator.hasNext()){
Map.Entry entry= (Map.Entry) iterator.next();
System.out.println(entry.getKey()+"-"+entry.getValue());
}
}
/**
*如果key对应的value存在,则替换为通过BiFunction计算得到的value,如果不存在则不存入
* @Param
*/
@Test
public void testComputeIfPresent()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
System.out.println(map.get("123"));
map.computeIfPresent("123",(key,val)-> key+"-"+val);
System.out.println(map.get("123"));
}
/**
* 无论key是否存在,都将BiFunction计算的值存入,存在则覆盖,不存在则新增
* @Param
*/
@Test
public void testCompute()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
System.out.println(map.get("123"));
map.compute("1234",(key,val)-> key+"-"+val);
System.out.println(map.get("1234"));
}
/**
* 如果key对应的值存在,则将BiFunction计算后的值替换调旧值,如果不存在则新增计算后的值
* @Param
*/
@Test
public void testMerge()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
System.out.println(map.get("123"));
map.merge("1234","1234",(oldVal,newVal)-> oldVal+"-"+newVal);
System.out.println(map.get("1234"));
}
/**
* 是否包含指定value值
* @Param
*/
@Test
public void testContains()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
System.out.println(map.contains("2"));
}
/**
* 通过枚举key遍历
* @Param
*/
@Test
public void testKeys()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
Enumeration enumeration=map.keys();
while (enumeration.hasMoreElements()){
String key=enumeration.nextElement();
System.out.println(key+":"+map.get(key));
}
}
/**
* 通过枚举遍历value
* @Param
*/
@Test
public void testElements()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
Enumeration values=map.elements();
while (values.hasMoreElements()){
System.out.println(values.nextElement());
}
}
/**
* 所有键值对映射的数量
* @Param
*/
@Test
public void testMappingCount()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
System.out.println(map.mappingCount());
}
/**
* 创建一个新的内部keysetview对象,其中包含一个新的concurrentHashMap和一个boolean值
* @Param
*/
@Test
public void testNewKeySet1()throws Exception{
System.out.println(ConcurrentHashMap.newKeySet().getMappedValue());
}
/**
*创建一个新的内部keysetview对象,其中包含一个给定容量的新的concurrentHashMap和一个boolean值
* @Param
*/
@Test
public void testNewKeySet()throws Exception{
System.out.println(ConcurrentHashMap.newKeySet(2).getMappedValue());
}
/**
* 通过keysetview遍历所有键值对
* @Param
*/
@Test
public void testKeySet()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
ConcurrentHashMap.KeySetView keySetView=map.keySet();
Iterator iterator=keySetView.iterator();
while (iterator.hasNext()){
String key=iterator.next();
System.out.println(key+":"+map.get(key));
}
}
/**
* 通过BIConsumer 遍历,第一个参数parallelismThreshold为并行子任务数量
* 用ForkJoinPool实现
* @Param
*/
@Test
public void testForEach1()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
map.forEach(1, (key,val) -> System.out.println(key+":"+val));
}
/**
*并行子任务数,通过BiConsumer处理key val的结果传给Consumer再次计算
* @Param
*/
@Test
public void testForEach()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
map.forEach(2,(key,val)-> key+"-"+val,key-> System.out.println(key+":"+map.get(key)) );
}
/**
* 通过Bifunction自己编写查找处理结果
* @Param
*/
@Test
public void testSearch()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
Integer ff=map.search(3, (key, val) -> {
if (key.equals("3")){
return 321321;
}
return null;
});
System.out.println(ff);
}
/**
*mapreduce计算
* @Param
*/
@Test
public void testReduce()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","31");
map.put("3","3");
map.put("123","2");
System.out.println(map.reduce(3,(key,val)->key+":"+val,(key,val)->key+"2"+val).toUpperCase());
}
/**
*并行执行, ToDoubleBiFunction将key和val转成double进行计算,
*连续加两次basis值,将结果放入DoubleBinaryOperator中进行计算返回结果
* @Param
*/
@Test
public void testReduceToDouble()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println( map.reduceToDouble(3,(key,val)->Double.parseDouble(key)+Double.parseDouble(val),2,((left, right) ->left+right )));
}
/**
*并行执行, ToLongBiFunction将key和val转成long进行计算,
*连续加两次basis值,将结果放入LongBinaryOperator中进行计算返回结果
*/
@Test
public void testReduceToLong()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println( map.reduceToLong(3,(key,val)->Long.parseLong(key)+Long.parseLong(val),2,((left, right) ->left+right )));
}
/**
*并行执行,ToIntBiFunction将key和val转成double进行计算,
*连续加两次basis值,将结果放入IntBinaryOperator中进行计算返回结果
* @Param
*/
@Test
public void testReduceToInt()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println( map.reduceToInt(3,(key,val)->Integer.parseInt(key)+Integer.parseInt(val),2,((left, right) ->left+right )));
}
/**
* 遍历所有key,并通过consumer消费key
* @Param
*/
@Test
public void testForEachKey1()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
map.forEachKey(3,s -> System.out.println(s+":"+map.get(s)));
}
/**
*遍历所有key,并将key传给Function计算,将结果传给consumer消费
* @Param
*/
@Test
public void testForEachKey()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
map.forEachKey(3,key -> map.get(key),s -> System.out.println(s+":"+map.get(s)));
}
/**
* 通过function 查找key进行处理对应的键值对
* @Param
*/
@Test
public void testSearchKeys()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
String dd=map.searchKeys(3,s -> {
if (s.equals("3")){
return map.get(s);
}
return null;
});
System.out.println(dd);
}
/**
* 取出所有key进行计算
* @Param
*/
@Test
public void testReduceKeys1()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println(map.reduceKeys(3, (key, val) -> key + ":" + val));
}
/**
*计算所有key,通过function,将结果传递给BiFunction并计算返回
* @Param
*/
@Test
public void testReduceKeys()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println(map.reduceKeys(3, key -> key + ":", (key, val) -> key + "--" + val).toUpperCase());
}
/**
*并行执行, ToDoubleFunction将key和val转成double进行计算,
*连续加两次basis值,将结果放入DoubleBinaryOperator中进行计算返回结果
* @Param
*/
@Test
public void testReduceKeysToDouble()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println( map.reduceKeysToDouble(3,(key)->Double.parseDouble(key),2,((left, right) ->left+right )));
}
/**
*并行执行, ToLongBiFunction将key和val转成long进行计算,
* 连续加两次basis值,将结果放入LongBinaryOperator中进行计算返回结果
* @Param
*/
@Test
public void testReduceKeysToLong()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println( map.reduceKeysToLong(3,(key)->Long.parseLong(key),2,((left, right) ->left+right )));
}
/**
*并行执行,ToIntBiFunction将key和val转成double进行计算,
*连续加两次basis值,将结果放入IntBinaryOperator中进行计算返回结果
* @Param
*/
@Test
public void testReduceKeysToInt()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println( map.reduceKeysToInt(3,(key)->Integer.parseInt(key),2,((left, right) ->left+right )));
}
/**
*遍历所有value,并将key传给Function计算,将结果传给consumer消费
* @Param
*/
@Test
public void testForEachValue1()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
map.forEachValue(3,s -> System.out.println(s+":"+map.get(s)));
}
/**
*遍历所有value,并将key传给Function计算,将结果传给consumer消费
* @Param
*/
@Test
public void testForEachValue()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
map.forEachValue(3,key -> key+"--",s -> System.out.println(s+":"+map.get(s)));
}
/**
*通过function 查找value进行处理返回结果
* @Param
*/
@Test
public void testSearchValues()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
Integer dd=map.searchValues(3,s -> {
if (s.equals("2")){
return 3243;
}
return null;
});
System.out.println(dd);
}
/**
*通过Bifunction规则计算并返回每次计算结果用于下次计算
* @Param
*/
@Test
public void testReduceValues1()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println(map.reduceValues(3, (val1, val2) -> val1 + "--"+val2));
}
/**
*计算所有value,通过function,将结果传递给BiFunction并计算返回
* @Param
*/
@Test
public void testReduceValues()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println(map.reduceValues(3, val -> val + ":", (key, val) -> key + "--" + val).toUpperCase());
}
/**
*并行执行, ToDoubleFunction将key和val转成double进行计算,
*连续加两次basis值,将结果放入DoubleBinaryOperator中进行计算返回结果
* @Param
*/
@Test
public void testReduceValuesToDouble()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println( map.reduceValuesToDouble(3,(key)->Double.parseDouble(key),2,((left, right) ->left+right )));
}
/**
*并行执行, ToLongBiFunction将key和val转成long进行计算,
* 连续加两次basis值,将结果放入LongBinaryOperator中进行计算返回结果
* @Param
*/
@Test
public void testReduceValuesToLong()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println( map.reduceValuesToLong(3,(key)->Long.parseLong(key),2,((left, right) ->left+right )));
}
/**
*并行执行,ToIntBiFunction将key和val转成double进行计算,
* 连续加两次basis值,将结果放入IntBinaryOperator中进行计算返回结果
* @Param
*/
@Test
public void testReduceValuesToInt()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println( map.reduceValuesToInt(3,(key)->Integer.parseInt(key),2,((left, right) ->left+right )));
}
/**
*遍历所有Entry,传给consumer消费
* @Param
*/
@Test
public void testForEachEntry1()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
map.forEachEntry(3,s -> System.out.println(s.getKey()+":"+s.getValue()));
}
/**
*遍历所有value,并将key传给Function计算,将结果传给consumer消费
* @Param
*/
@Test
public void testForEachEntry()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
map.forEachEntry(3,key -> key+"--",s -> System.out.println(s+":"+map.get(s)));
}
/**
*通过function 查找entry进行处理返回结果
* @Param
*/
@Test
public void testSearchEntries()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
String dd=map.searchEntries(3,s -> {
if (s.getValue().equals("2")){
return s.getKey();
}
return null;
});
System.out.println(dd);
}
/**
*通过Bifunction规则计算并返回每次计算结果用于下次计算
* @Param
*/
@Test
public void testReduceEntries1()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println(map.reduceEntries(3, (val1, val2) -> val1));
}
/**
*计算所有entry,通过function,将结果传递给BiFunction并计算返回
* @Param
*/
@Test
public void testReduceEntries()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println(map.reduceEntries(3, val -> val + ":", (key, val) -> key + "--" + val).toUpperCase());
}
/**
*并行执行, ToDoubleFunction将key和val转成double进行计算,
*连续加两次basis值,将结果放入DoubleBinaryOperator中进行计算返回结果
* @Param
*/
@Test
public void testReduceEntriesToDouble()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println( map.reduceEntriesToDouble(3,entry->Double.parseDouble(entry.getKey()),2,((left, right) ->left+right )));
}
/**
*并行执行, ToLongBiFunction将key和val转成long进行计算,
*连续加两次basis值,将结果放入LongBinaryOperator中进行计算返回结果
* @Param
*/
@Test
public void testReduceEntriesToLong()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println( map.reduceEntriesToLong(3,(entry)->Long.parseLong(entry.getValue()),2,((left, right) ->left+right )));
}
/**
*并行执行,ToIntBiFunction将key和val转成double进行计算,
* 连续加两次basis值,将结果放入IntBinaryOperator中进行计算返回结果
* @Param
*/
@Test
public void testReduceEntriesToInt()throws Exception{
ConcurrentHashMap map=new ConcurrentHashMap();
map.put("1","2");
map.put("3","4");
map.put("5","6");
map.put("7","8");
map.put("9","10");
System.out.println( map.reduceEntriesToInt(3,(entry)->Integer.parseInt(entry.getValue()),2,((left, right) ->left+right )));
}
}