本文整理汇总了Java中java.util.concurrent.ConcurrentHashMap.merge方法的典型用法代码示例。如果您正苦于以下问题:Java ConcurrentHashMap.merge方法的具体用法?Java ConcurrentHashMap.merge怎么用?Java ConcurrentHashMap.merge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ConcurrentHashMap
的用法示例。
在下文中一共展示了ConcurrentHashMap.merge方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testMerge3
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
/**
* merge removes when the given key is present and function returns null
*/
public void testMerge3() {
ConcurrentHashMap map = map5();
map.merge(one, "Y", (x, y) -> null);
assertFalse(map.containsKey(one));
}
示例2: initClusterCenters
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
/** Initialize cluster centers. */
private Vector[] initClusterCenters(SparseDistributedMatrix points, int k) {
// Initialize empty centers and point costs.
int ptsCnt = points.rowSize();
String cacheName = ((SparseDistributedMatrixStorage)points.getStorage()).cacheName();
// Initialize the first center to a random point.
Vector sample = localCopyOf(points.viewRow(rnd.nextInt(ptsCnt)));
List<Vector> centers = new ArrayList<>();
List<Vector> newCenters = new ArrayList<>();
newCenters.add(sample);
centers.add(sample);
final ConcurrentHashMap<Integer, Double> costs = new ConcurrentHashMap<>();
// On each step, sample 2 * k points on average with probability proportional
// to their squared distance from the centers. Note that only distances between points
// and new centers are computed in each iteration.
int step = 0;
UUID uid = points.getUUID();
while (step < initSteps) {
// We assume here that costs can fit into memory of one node.
ConcurrentHashMap<Integer, Double> newCosts = getNewCosts(points, newCenters, cacheName);
// Merge costs with new costs.
for (Integer ind : newCosts.keySet())
costs.merge(ind, newCosts.get(ind), Math::min);
double sumCosts = costs.values().stream().mapToDouble(Double::valueOf).sum();
newCenters = getNewCenters(k, costs, uid, sumCosts, cacheName);
centers.addAll(newCenters);
step++;
}
List<Vector> distinctCenters = centers.stream().distinct().collect(Collectors.toList());
if (distinctCenters.size() <= k)
return distinctCenters.toArray(new Vector[] {});
else {
// Finally, we might have a set of more than k distinct candidate centers; weight each
// candidate by the number of points in the dataset mapping to it and run a local k-means++
// on the weighted centers to pick k of them
ConcurrentHashMap<Integer, Integer> centerInd2Weight = weightCenters(uid, distinctCenters, cacheName);
List<Double> weights = new ArrayList<>(centerInd2Weight.size());
for (int i = 0; i < distinctCenters.size(); i++)
weights.add(i, Double.valueOf(centerInd2Weight.getOrDefault(i, 0)));
DenseLocalOnHeapMatrix dCenters = MatrixUtil.fromList(distinctCenters, true);
return new KMeansLocalClusterer(getDistanceMeasure(), 30, seed).cluster(dCenters, k, weights).centers();
}
}
示例3: initializeCenters
import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
/**
* Choose k primary centers from source points.
*
* @param points Matrix with source points.
* @param k Number of centers.
* @return Array of primary centers.
*/
private Vector[] initializeCenters(SparseDistributedMatrix points, int k) {
int pointsNum = points.rowSize();
Vector firstCenter = points.viewRow(rnd.nextInt(pointsNum));
List<Vector> centers = new ArrayList<>();
List<Vector> newCenters = new ArrayList<>();
centers.add(firstCenter);
newCenters.add(firstCenter);
ConcurrentHashMap<Integer, Double> costs = new ConcurrentHashMap<>();
int step = 0;
UUID uuid = points.getUUID();
String cacheName = ((SparseDistributedMatrixStorage) points.getStorage()).cacheName();
while(step < initSteps) {
ConcurrentHashMap<Integer, Double> newCosts = getNewCosts(cacheName, uuid, newCenters);
for (Integer key : newCosts.keySet())
costs.merge(key, newCosts.get(key), Math::min);
double costsSum = costs.values().stream().mapToDouble(Double::valueOf).sum();
newCenters = getNewCenters(cacheName, uuid, costs, costsSum, k);
centers.addAll(newCenters);
step++;
}
return chooseKCenters(cacheName, uuid, centers, k);
}