當前位置: 首頁>>代碼示例>>Java>>正文


Java ConcurrentHashMap.merge方法代碼示例

本文整理匯總了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));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:9,代碼來源:ConcurrentHashMap8Test.java

示例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();
    }
}
 
開發者ID:Luodian,項目名稱:Higher-Cloud-Computing-Project,代碼行數:60,代碼來源:KMeansDistributedClusterer.java

示例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);
}
 
開發者ID:Luodian,項目名稱:Higher-Cloud-Computing-Project,代碼行數:41,代碼來源:FuzzyCMeansDistributedClusterer.java


注:本文中的java.util.concurrent.ConcurrentHashMap.merge方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。