我有一张地图如下 -
ConcurrentHashMap<Long, AtomicLong> histogram = new ConcurrentHashMap<Long, AtomicLong>();
该映射包含很多键值对。将AtomicLong
值作为值是必要的,所以这就是我在该地图中放置这样的原因。
所以现在如果我试图遍历ConcurrentHashMap
我上面提到的,它总是在这条线上给我错误-
buckets[i] += histogram.get(time);
作为-The operator += is undefined for the argument type(s) int, AtomicLong
我不确定如何解决此问题?我无法返回并将数据结构更改为 allInteger
而不是Long
and AtomicLong
。
所以我需要找出如何在下面的代码中进行更改,以便它开始工作。我正在考虑转换为整数。但它也没有工作。
下面是我的代码
private static void logHistogramInfo() {
System.out.println("From Main Thread: " + histogram);
int[] definition = { 0, 20, 40, 60, 80, 100 };
int[] buckets = new int[definition.length];
for (Long time : histogram.keySet()) {
for (int i = definition.length - 1; i >= 0; i--) {
if (time >= definition[i]) {
//error on the below line
buckets[i] += histogram.get(time);
break;
}
}
}
for (int i = 0; i < definition.length; i++) {
String period = "";
if (i == definition.length - 1) {
period = "greater than " + definition[i] + "ms";
} else {
period = "between " + (definition[i] + 1) + " and " + definition[i + 1] + "ms";
}
System.out.println(buckets[i] + " came back " + period);
}
}