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

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

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

Map.computeIfPresent介绍

暂无

代码示例

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

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

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

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

代码示例来源:origin: neo4j/neo4j

private void cleanupMonitorListeners( Object monitorListener, Method key )
{
  methodMonitorListeners.computeIfPresent( key, ( method1, handlers ) ->
  {
    handlers.removeIf( handler -> monitorListener.equals( handler.getMonitorListener() ) );
    return handlers.isEmpty() ? null : handlers;
  } );
}

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

public void removeSegment(String serverName, DataSegment dataSegment)
{
 serversCostCache.computeIfPresent(
   serverName,
   (s, builder) -> builder.removeSegment(dataSegment).isEmpty() ? null : builder
 );
}

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

@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
public void testComputeIfPresent_nullKeySupportedAbsent() {
 assertNull(
   "computeIfPresent(null, function) should return null",
   getMap()
     .computeIfPresent(
       null,
       (k, v) -> {
        throw new AssertionFailedError();
       }));
 expectUnchanged();
}

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

@MapFeature.Require(absent = SUPPORTS_PUT)
public void testComputeIfPresent_unsupportedAbsent() {
 try {
  getMap()
    .computeIfPresent(
      k3(),
      (k, v) -> {
       throw new AssertionFailedError();
      });
 } catch (UnsupportedOperationException tolerated) {
 }
 expectUnchanged();
}

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

@MapFeature.Require(SUPPORTS_PUT)
public void testComputeIfPresent_supportedAbsent() {
 assertNull(
   "computeIfPresent(notPresent, function) should return null",
   getMap()
     .computeIfPresent(
       k3(),
       (k, v) -> {
        throw new AssertionFailedError();
       }));
 expectUnchanged();
}

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

@Override public Integer apply(Integer key, Integer value) {
  if (recursed) {
   throw new StackOverflowError();
  }
  recursed = true;
  return map.computeIfPresent(key, this);
 }
};

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

public Builder removeSegment(DataSegment dataSegment)
{
 allSegmentsCostCache.removeSegment(dataSegment);
 segmentsPerDataSource.computeIfPresent(
   dataSegment.getDataSource(),
   (ds, builder) -> builder.removeSegment(dataSegment).isEmpty() ? null : builder
 );
 return this;
}

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

@CheckNoWriter @CheckNoStats
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void computeIfPresent_absent(Map<Integer, Integer> map, CacheContext context) {
 assertThat(map.computeIfPresent(context.absentKey(), (key, value) -> value), is(nullValue()));
 assertThat(map.get(context.absentKey()), is(nullValue()));
 assertThat(map.size(), is(context.original().size()));
}

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

@MapFeature.Require(absent = SUPPORTS_PUT)
 @CollectionSize.Require(absent = ZERO)
 public void testComputeIfPresent_unsupportedPresent() {
  try {
   getMap().computeIfPresent(k0(), (k, v) -> v3());
   fail("Expected UnsupportedOperationException");
  } catch (UnsupportedOperationException expected) {
  }
  expectUnchanged();
 }
}

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

@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
@CollectionSize.Require(absent = ZERO)
public void testComputeIfPresent_nullTreatedAsAbsent() {
 initMapWithNullValue();
 assertNull(
   "computeIfPresent(presentAssignedToNull, function) should return null",
   getMap()
     .computeIfPresent(
       getKeyForNullValue(),
       (k, v) -> {
        throw new AssertionFailedError();
       }));
 expectReplacement(entry(getKeyForNullValue(), null));
}

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

@Override public Integer apply(Integer key, Integer value) {
  if (++recursed == 2) {
   throw new StackOverflowError();
  }
  return map.computeIfPresent(context.lastKey(), this);
 }
};

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

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

代码示例来源: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: ben-manes/caffeine

@Test(dataProvider = "caches")
@CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL })
public void computeIfPresent_error(Map<Integer, Integer> map, CacheContext context) {
 try {
  map.computeIfPresent(context.firstKey(), (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.firstKey(), (k, v) -> -k), is(-context.firstKey()));
}

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

@MapFeature.Require(SUPPORTS_PUT)
@CollectionSize.Require(absent = ZERO)
public void testComputeIfPresent_functionReturnsNull() {
 assertNull(
   "computeIfPresent(present, returnsNull) should return null",
   getMap()
     .computeIfPresent(
       k0(),
       (k, v) -> {
        assertEquals(k0(), k);
        assertEquals(v0(), v);
        return null;
       }));
 expectMissing(e0());
}

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

@MapFeature.Require(SUPPORTS_PUT)
@CollectionSize.Require(absent = ZERO)
public void testComputeIfPresent_functionThrows() {
 try {
  getMap()
    .computeIfPresent(
      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)
@CollectionSize.Require(absent = ZERO)
public void testComputeIfPresent_supportedPresent() {
 assertEquals(
   "computeIfPresent(present, function) should return new value",
   v3(),
   getMap()
     .computeIfPresent(
       k0(),
       (k, v) -> {
        assertEquals(k0(), k);
        assertEquals(v0(), v);
        return v3();
       }));
 expectReplacement(entry(k0(), v3()));
}

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

@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
@CollectionSize.Require(absent = ZERO)
public void testComputeIfPresent_nullKeySupportedPresent() {
 initMapWithNullKey();
 assertEquals(
   "computeIfPresent(null, function) should return new value",
   v3(),
   getMap()
     .computeIfPresent(
       null,
       (k, v) -> {
        assertNull(k);
        assertEquals(getValueForNullKey(), v);
        return v3();
       }));
 Entry<K, V>[] expected = createArrayWithNullKey();
 expected[getNullLocation()] = entry(null, v3());
 expectContents(expected);
}

相关文章