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

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

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

Map.merge介绍

暂无

代码示例

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

public void addAssignedSplit(Node node)
  {
    assignmentCount.merge(node, 1, (x, y) -> x + y);
  }
}

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

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

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

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

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

@Override
public void updateCount(Tuple2<Long, IntType> value, Map<Long, Integer> windowCounts) {
  windowCounts.merge(value.f0, value.f1.value, (a, b) -> a + b);
}

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

public static Map<PlanNodeId, PlanNodeStats> aggregatePlanNodeStats(List<StageInfo> stageInfos)
{
  Map<PlanNodeId, PlanNodeStats> aggregatedStats = new HashMap<>();
  List<PlanNodeStats> planNodeStats = stageInfos.stream()
      .flatMap(stageInfo -> stageInfo.getTasks().stream())
      .map(TaskInfo::getStats)
      .flatMap(taskStats -> getPlanNodeStats(taskStats).stream())
      .collect(toList());
  for (PlanNodeStats stats : planNodeStats) {
    aggregatedStats.merge(stats.getPlanNodeId(), stats, (left, right) -> left.mergeWith(right));
  }
  return aggregatedStats;
}

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

public void testMergeNullValue() {
 try {
  getMap()
    .merge(
      k0(),
      null,
      (oldV, newV) -> {
       throw new AssertionFailedError("Should not call merge function if value was null");
      });
  fail("Expected NullPointerException or UnsupportedOperationException");
 } catch (NullPointerException | UnsupportedOperationException expected) {
 }
}

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

@Override
public void validate(PlanNode planNode, Session session, Metadata metadata, SqlParser sqlParser, TypeProvider types, WarningCollector warningCollector)
{
  Map<PlanNodeId, PlanNode> planNodeIds = new HashMap<>();
  searchFrom(planNode)
      .findAll()
      .forEach(node -> planNodeIds.merge(node.getId(), node, this::reportDuplicateId));
}

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

public void testMergeNullFunction() {
 try {
  getMap().merge(k0(), v3(), null);
  fail("Expected NullPointerException or UnsupportedOperationException");
 } catch (NullPointerException | UnsupportedOperationException expected) {
 }
}

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

@MapFeature.Require(absent = SUPPORTS_PUT)
public void testMergeUnsupported() {
 try {
  getMap()
    .merge(
      k3(),
      v3(),
      (oldV, newV) -> {
       throw new AssertionFailedError();
      });
  fail("Expected UnsupportedOperationException");
 } catch (UnsupportedOperationException expected) {
 }
}

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

private void updateRowsOnHosts(MemoryTableHandle table, Collection<Slice> fragments)
{
  checkState(
      tableDataFragments.containsKey(table.getTableId()),
      "Uninitialized table [%s.%s]",
      table.getSchemaName(),
      table.getTableName());
  Map<HostAddress, MemoryDataFragment> dataFragments = tableDataFragments.get(table.getTableId());
  for (Slice fragment : fragments) {
    MemoryDataFragment memoryDataFragment = MemoryDataFragment.fromSlice(fragment);
    dataFragments.merge(memoryDataFragment.getHostAddress(), memoryDataFragment, MemoryDataFragment::merge);
  }
}

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

@Override
  public void updateCount(Tuple4<Long, Long, Long, IntType> value, Map<Long, Integer> windowCounts) {
    windowCounts.merge(value.f0, 1, (a, b) -> a + b);
    // verify the contents of that window, the contents should be:
    // (key + num windows so far)
    assertEquals("Window counts don't match for key " + value.f0 + ".", value.f0.intValue() + windowCounts.get(value.f0), value.f3.value);
  }
}

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

@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS})
public void testMergeAbsentNullKey() {
 assertEquals(
   "Map.merge(null, value, function) should return value",
   v3(),
   getMap()
     .merge(
       null,
       v3(),
       (oldV, newV) -> {
        throw new AssertionFailedError(
          "Should not call merge function if key was absent");
       }));
 expectAdded(entry(null, v3()));
}

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

@MapFeature.Require(SUPPORTS_PUT)
public void testAbsent() {
 assertEquals(
   "Map.merge(absent, value, function) should return value",
   v3(),
   getMap()
     .merge(
       k3(),
       v3(),
       (oldV, newV) -> {
        throw new AssertionFailedError(
          "Should not call merge function if key was absent");
       }));
 expectAdded(e3());
}

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

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

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

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

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

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

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

@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_VALUES})
@CollectionSize.Require(absent = ZERO)
public void testMappedToNull() {
 initMapWithNullValue();
 assertEquals(
   "Map.merge(keyMappedToNull, value, function) should return value",
   v3(),
   getMap()
     .merge(
       getKeyForNullValue(),
       v3(),
       (oldV, newV) -> {
        throw new AssertionFailedError(
          "Should not call merge function if key was mapped to null");
       }));
 expectReplacement(entry(getKeyForNullValue(), v3()));
}

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

@MapFeature.Require(SUPPORTS_REMOVE)
@CollectionSize.Require(absent = ZERO)
public void testMergePresentToNull() {
 assertNull(
   "Map.merge(present, value, functionReturningNull) should return null",
   getMap()
     .merge(
       k0(),
       v3(),
       (oldV, newV) -> {
        assertEquals(v0(), oldV);
        assertEquals(v3(), newV);
        return null;
       }));
 expectMissing(e0());
}

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

@MapFeature.Require(SUPPORTS_PUT)
@CollectionSize.Require(absent = ZERO)
public void testMergeFunctionThrows() {
 try {
  getMap()
    .merge(
      k0(),
      v3(),
      (oldV, newV) -> {
       assertEquals(v0(), oldV);
       assertEquals(v3(), newV);
       throw new ExpectedException();
      });
  fail("Expected ExpectedException");
 } catch (ExpectedException expected) {
 }
 expectUnchanged();
}

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

@MapFeature.Require(SUPPORTS_PUT)
@CollectionSize.Require(absent = ZERO)
public void testMergePresent() {
 assertEquals(
   "Map.merge(present, value, function) should return function result",
   v4(),
   getMap()
     .merge(
       k0(),
       v3(),
       (oldV, newV) -> {
        assertEquals(v0(), oldV);
        assertEquals(v3(), newV);
        return v4();
       }));
 expectReplacement(entry(k0(), v4()));
}

相关文章