java.util.Map.compute()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(181)

本文整理了Java中java.util.Map.compute()方法的一些代码示例,展示了Map.compute()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Map.compute()方法的具体详情如下:
包路径:java.util.Map
类名称:Map
方法名:compute

Map.compute介绍

暂无

代码示例

代码示例来源:origin: prestodb/presto

private int getAndIncrementPartitionIndex(Lifespan lifespan)
  {
    return partitionIndexManager.compute(lifespan, (k, v) -> v == null ? 1 : v + 1) - 1;
  }
}

代码示例来源:origin: ben-manes/caffeine

@CacheSpec
@Test(dataProvider = "caches")
public void compute_error(Map<Integer, Integer> map, CacheContext context) {
 try {
  map.compute(context.absentKey(), (key, value) -> { throw new Error(); });
 } catch (Error e) {}
 assertThat(map, is(equalTo(context.original())));
 assertThat(context, both(hasMissCount(0)).and(hasHitCount(0)));
 assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1)));
 assertThat(map.computeIfPresent(context.absentKey(), (k, v) -> -k), is(nullValue()));
}

代码示例来源:origin: apache/incubator-druid

public void reduceLifetime(String tier)
{
 lifetimes.compute(
   tier,
   (t, lifetime) -> {
    if (lifetime == null) {
     return maxLifetime - 1;
    }
    return lifetime - 1;
   }
 );
}

代码示例来源:origin: google/guava

@Override
public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
 synchronized (mutex) {
  return delegate().compute(key, remappingFunction);
 }
}

代码示例来源:origin: apache/storm

private void pin(K key) {
  LOG.debug("pin '{}'", key);
  pinned.compute(key, (k, v) -> v == null ? 1L : v + 1);
  LOG.debug("pinned '{}'", pinned);
}

代码示例来源:origin: prestodb/presto

@Override
public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
 synchronized (mutex) {
  return delegate().compute(key, remappingFunction);
 }
}

代码示例来源:origin: apache/incubator-druid

private static void convertRowTypesToOutputTypes(List<DimensionSpec> dimensionSpecs, Map<String, Object> rowMap)
{
 for (DimensionSpec dimSpec : dimensionSpecs) {
  final ValueType outputType = dimSpec.getOutputType();
  rowMap.compute(
    dimSpec.getOutputName(),
    (dimName, baseVal) -> DimensionHandlerUtils.convertObjectToType(baseVal, outputType)
  );
 }
}

代码示例来源:origin: ben-manes/caffeine

@CheckNoWriter @CheckNoStats
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void compute_nullKey(Map<Integer, Integer> map, CacheContext context) {
 map.compute(null, (key, value) -> -key);
}

代码示例来源:origin: ben-manes/caffeine

@CheckNoWriter @CheckNoStats
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
@Test(dataProvider = "caches", expectedExceptions = NullPointerException.class)
public void compute_nullMappingFunction(Map<Integer, Integer> map, CacheContext context) {
 map.compute(1, null);
}

代码示例来源:origin: google/guava

@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
public void testCompute_absentToAbsent() {
 assertNull(
   "Map.compute(absent, functionReturningNull) should return null",
   getMap()
     .compute(
       k3(),
       (k, v) -> {
        assertEquals(k3(), k);
        assertNull(v);
        return null;
       }));
 expectUnchanged();
 assertEquals(getNumElements(), getMap().size());
}

代码示例来源:origin: ben-manes/caffeine

@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void compute_absent_nullValue(Map<Integer, Integer> map, CacheContext context) {
 assertThat(map.compute(context.absentKey(), (key, value) -> null), is(nullValue()));
 assertThat(context, both(hasMissCount(0)).and(hasHitCount(0)));
 assertThat(context, both(hasLoadSuccessCount(0)).and(hasLoadFailureCount(1)));
 assertThat(map.get(context.absentKey()), is(nullValue()));
 assertThat(map.size(), is(context.original().size()));
}

代码示例来源:origin: google/guava

@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
 public void testCompute_absentFunctionThrows() {
  try {
   getMap()
     .compute(
       k3(),
       (k, v) -> {
        assertEquals(k3(), k);
        assertNull(v);
        throw new ExpectedException();
       });
   fail("Expected ExpectedException");
  } catch (ExpectedException expected) {
  }
  expectUnchanged();
 }
}

代码示例来源:origin: ben-manes/caffeine

@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void compute_absent(Map<Integer, Integer> map, CacheContext context) {
 assertThat(map.compute(context.absentKey(),
   (key, value) -> context.absentValue()), is(context.absentValue()));
 assertThat(context, both(hasMissCount(0)).and(hasHitCount(0)));
 assertThat(context, both(hasLoadSuccessCount(1)).and(hasLoadFailureCount(0)));
 assertThat(map.get(context.absentKey()), is(context.absentValue()));
 assertThat(map.size(), is(1 + context.original().size()));
}

代码示例来源:origin: google/guava

@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_KEYS})
@CollectionSize.Require(absent = ZERO)
public void testCompute_nullKeyPresentToPresent() {
 initMapWithNullKey();
 assertEquals(
   "Map.compute(present, functionReturningValue) should return new value",
   v3(),
   getMap()
     .compute(
       null,
       (k, v) -> {
        assertNull(k);
        assertEquals(getValueForNullKey(), v);
        return v3();
       }));
 assertEquals(getNumElements(), getMap().size());
}

代码示例来源:origin: google/guava

@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
public void testCompute_absentToPresent() {
 assertEquals(
   "Map.compute(absent, functionReturningValue) should return value",
   v3(),
   getMap()
     .compute(
       k3(),
       (k, v) -> {
        assertEquals(k3(), k);
        assertNull(v);
        return v3();
       }));
 expectAdded(e3());
 assertEquals(getNumElements() + 1, getMap().size());
}

代码示例来源:origin: google/guava

@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
@CollectionSize.Require(absent = ZERO)
public void testCompute_presentNullToNull() {
 // The spec is somewhat ambiguous about this case, but the actual default implementation
 // in Map will remove a present null.
 initMapWithNullValue();
 assertNull(
   "Map.compute(presentMappedToNull, functionReturningNull) should return null",
   getMap()
     .compute(
       getKeyForNullValue(),
       (k, v) -> {
        assertEquals(getKeyForNullValue(), k);
        assertNull(v);
        return null;
       }));
 expectMissingKeys(getKeyForNullValue());
 assertEquals(getNumElements() - 1, getMap().size());
}

代码示例来源:origin: google/guava

@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
@CollectionSize.Require(absent = ZERO)
public void testCompute_presentFunctionThrows() {
 try {
  getMap()
    .compute(
      k0(),
      (k, v) -> {
       assertEquals(k0(), k);
       assertEquals(v0(), v);
       throw new ExpectedException();
      });
  fail("Expected ExpectedException");
 } catch (ExpectedException expected) {
 }
 expectUnchanged();
}

代码示例来源:origin: google/guava

@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
@CollectionSize.Require(absent = ZERO)
public void testCompute_presentToAbsent() {
 assertNull(
   "Map.compute(present, functionReturningNull) should return null",
   getMap()
     .compute(
       k0(),
       (k, v) -> {
        assertEquals(k0(), k);
        assertEquals(v0(), v);
        return null;
       }));
 expectMissing(e0());
 expectMissingKeys(k0());
 assertEquals(getNumElements() - 1, getMap().size());
}

代码示例来源:origin: google/guava

@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE, ALLOWS_NULL_VALUES})
@CollectionSize.Require(absent = ZERO)
public void testCompute_presentNullToPresentNonnull() {
 initMapWithNullValue();
 V value = getValueForNullKey();
 assertEquals(
   "Map.compute(presentMappedToNull, functionReturningValue) should return new value",
   value,
   getMap()
     .compute(
       getKeyForNullValue(),
       (k, v) -> {
        assertEquals(getKeyForNullValue(), k);
        assertNull(v);
        return value;
       }));
 expectReplacement(entry(getKeyForNullValue(), value));
 assertEquals(getNumElements(), getMap().size());
}

代码示例来源:origin: google/guava

@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE})
@CollectionSize.Require(absent = ZERO)
public void testCompute_presentToPresent() {
 assertEquals(
   "Map.compute(present, functionReturningValue) should return new value",
   v3(),
   getMap()
     .compute(
       k0(),
       (k, v) -> {
        assertEquals(k0(), k);
        assertEquals(v0(), v);
        return v3();
       }));
 expectReplacement(entry(k0(), v3()));
 assertEquals(getNumElements(), getMap().size());
}

相关文章