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

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

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

Map.remove介绍

[英]Removes a mapping with the specified key from this Map.
[中]从此映射中删除具有指定键的映射。

代码示例

代码示例来源:origin: spring-projects/spring-framework

public void setAttribute(String name, Object value) {
  if (value != null) {
    this.attributes.put(name, value);
  }
  else {
    this.attributes.remove(name);
  }
}

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

@Override
public Set<V> removeAll(Object key) {
 Set<V> values = new HashSet<V>(2);
 if (!map.containsKey(key)) {
  return values;
 }
 values.add(map.remove(key));
 return values;
}

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

private boolean isMuted(TopicPartition tp, long now) {
  boolean result = muted.containsKey(tp) && muted.get(tp) > now;
  if (!result)
    muted.remove(tp);
  return result;
}

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

@Override
public V put(K key, V value) {
  if (keyValueMap.containsKey(key)) {
    valueKeyMap.remove(keyValueMap.get(key));
  }
  valueKeyMap.put(value, key);
  return keyValueMap.put(key, value);
}

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

@Override
  public void run() {
    try {
      CompletableFuture<Monitor> completableFuture = AbstractMonitorFactory.FUTURES.get(key);
      AbstractMonitorFactory.MONITORS.put(key, completableFuture.get());
      AbstractMonitorFactory.FUTURES.remove(key);
    } catch (InterruptedException e) {
      logger.warn("Thread was interrupted unexpectedly, monitor will never be got.");
      AbstractMonitorFactory.FUTURES.remove(key);
    } catch (ExecutionException e) {
      logger.warn("Create monitor failed, monitor data will not be collected until you fix this problem. ", e);
    }
  }
}

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

private static Set<WorkerSlot> badSlots(Map<WorkerSlot, List<ExecutorDetails>> existingSlots, int numExecutors, int numWorkers) {
  if (numWorkers != 0) {
    Map<Integer, Integer> distribution = Utils.integerDivided(numExecutors, numWorkers);
    Set<WorkerSlot> slots = new HashSet<WorkerSlot>();
    for (Entry<WorkerSlot, List<ExecutorDetails>> entry : existingSlots.entrySet()) {
      Integer executorCount = entry.getValue().size();
      Integer workerCount = distribution.get(executorCount);
      if (workerCount != null && workerCount > 0) {
        slots.add(entry.getKey());
        workerCount--;
        distribution.put(executorCount, workerCount);
      }
    }
    for (WorkerSlot slot : slots) {
      existingSlots.remove(slot);
    }
    return existingSlots.keySet();
  }
  return null;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Add the given singleton factory for building the specified singleton
 * if necessary.
 * <p>To be called for eager registration of singletons, e.g. to be able to
 * resolve circular references.
 * @param beanName the name of the bean
 * @param singletonFactory the factory for the singleton object
 */
protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
  Assert.notNull(singletonFactory, "Singleton factory must not be null");
  synchronized (this.singletonObjects) {
    if (!this.singletonObjects.containsKey(beanName)) {
      this.singletonFactories.put(beanName, singletonFactory);
      this.earlySingletonObjects.remove(beanName);
      this.registeredSingletons.add(beanName);
    }
  }
}

代码示例来源:origin: twosigma/beakerx

public void dropAll(final String shellId) {
  List<String> keys = new ArrayList<String>(_shellToId.keySet());
  for (String s : keys) {
   if (_shellToId.get(s).equals(shellId)) {
    _shellToId.remove(s);
    _forms.remove(s);
   }
  }
 }
}

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

public void testRemove() {
 final Map<K, V> map;
 final K keyToRemove;
 try {
  map = makePopulatedMap();
 } catch (UnsupportedOperationException e) {
  return;
 }
 keyToRemove = map.keySet().iterator().next();
 if (supportsRemove) {
  int initialSize = map.size();
  V expectedValue = map.get(keyToRemove);
  V oldValue = map.remove(keyToRemove);
  assertEquals(expectedValue, oldValue);
  assertFalse(map.containsKey(keyToRemove));
  assertEquals(initialSize - 1, map.size());
 } else {
  try {
   map.remove(keyToRemove);
   fail("Expected UnsupportedOperationException.");
  } catch (UnsupportedOperationException expected) {
  }
 }
 assertInvariants(map);
}

代码示例来源:origin: skylot/jadx

public void remove(IAttribute attr) {
  AType<? extends IAttribute> type = attr.getType();
  IAttribute a = attributes.get(type);
  if (a == attr) {
    attributes.remove(type);
  }
}

代码示例来源:origin: org.testng/testng

@Override
public void removeResult(ITestNGMethod m) {
 for (Entry<ITestResult, ITestNGMethod> entry : m_map.entrySet()) {
  if (entry.getValue().equals(m)) {
   m_map.remove(entry.getKey());
   return;
  }
 }
}

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

private static <K, V> void doDifference(
  Map<? extends K, ? extends V> left,
  Map<? extends K, ? extends V> right,
  Equivalence<? super V> valueEquivalence,
  Map<K, V> onlyOnLeft,
  Map<K, V> onlyOnRight,
  Map<K, V> onBoth,
  Map<K, MapDifference.ValueDifference<V>> differences) {
 for (Entry<? extends K, ? extends V> entry : left.entrySet()) {
  K leftKey = entry.getKey();
  V leftValue = entry.getValue();
  if (right.containsKey(leftKey)) {
   V rightValue = onlyOnRight.remove(leftKey);
   if (valueEquivalence.equivalent(leftValue, rightValue)) {
    onBoth.put(leftKey, leftValue);
   } else {
    differences.put(leftKey, ValueDifferenceImpl.create(leftValue, rightValue));
   }
  } else {
   onlyOnLeft.put(leftKey, leftValue);
  }
 }
}

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

public synchronized void purgeUnused() {
  Iterator<Map.Entry<Long, AtomicLongWithEquals>> refCountIter = referenceCounter.entrySet().iterator();
  while (refCountIter.hasNext()) {
    Map.Entry<Long, AtomicLongWithEquals> entry = refCountIter.next();
    if (entry.getValue().get() <= 0) {
      Long acl = entry.getKey();
      aclKeyMap.remove(longKeyMap.get(acl));
      longKeyMap.remove(acl);
      refCountIter.remove();
    }
  }
}

代码示例来源:origin: ctripcorp/apollo

/**
 * 合并用户对namespace的修改
 */
private Map<String, String> mergeOverriddenProperties(String namespace, Map<String, String> configurations) {
 if (addedOrModifiedPropertiesOfNamespace.containsKey(namespace)) {
  configurations.putAll(addedOrModifiedPropertiesOfNamespace.get(namespace));
 }
 if (deletedKeysOfNamespace.containsKey(namespace)) {
  for (String k : deletedKeysOfNamespace.get(namespace)) {
   configurations.remove(k);
  }
 }
 return configurations;
}

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

@CanIgnoreReturnValue
private Map<R, V> removeColumn(Object column) {
 Map<R, V> output = new LinkedHashMap<>();
 Iterator<Entry<R, Map<C, V>>> iterator = backingMap.entrySet().iterator();
 while (iterator.hasNext()) {
  Entry<R, Map<C, V>> entry = iterator.next();
  V value = entry.getValue().remove(column);
  if (value != null) {
   output.put(entry.getKey(), value);
   if (entry.getValue().isEmpty()) {
    iterator.remove();
   }
  }
 }
 return output;
}

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

public void testStandardEntrySet() throws InvocationTargetException {
 @SuppressWarnings("unchecked")
 final Map<String, Boolean> map = mock(Map.class);
 Map<String, Boolean> forward =
   new ForwardingMap<String, Boolean>() {
    @Override
    protected Map<String, Boolean> delegate() {
     return map;
    }
    @Override
    public Set<Entry<String, Boolean>> entrySet() {
     return new StandardEntrySet() {
      @Override
      public Iterator<Entry<String, Boolean>> iterator() {
       return Iterators.emptyIterator();
      }
     };
    }
   };
 callAllPublicMethods(new TypeToken<Set<Entry<String, Boolean>>>() {}, forward.entrySet());
 // These are the methods specified by StandardEntrySet
 verify(map, atLeast(0)).clear();
 verify(map, atLeast(0)).containsKey(anyObject());
 verify(map, atLeast(0)).get(anyObject());
 verify(map, atLeast(0)).isEmpty();
 verify(map, atLeast(0)).remove(anyObject());
 verify(map, atLeast(0)).size();
 verifyNoMoreInteractions(map);
}

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

public void testStandardKeySet() throws InvocationTargetException {
 @SuppressWarnings("unchecked")
 final Map<String, Boolean> map = mock(Map.class);
 Map<String, Boolean> forward =
   new ForwardingMap<String, Boolean>() {
    @Override
    protected Map<String, Boolean> delegate() {
     return map;
    }
    @Override
    public Set<String> keySet() {
     return new StandardKeySet();
    }
   };
 callAllPublicMethods(new TypeToken<Set<String>>() {}, forward.keySet());
 // These are the methods specified by StandardKeySet
 verify(map, atLeast(0)).clear();
 verify(map, atLeast(0)).containsKey(anyObject());
 verify(map, atLeast(0)).isEmpty();
 verify(map, atLeast(0)).remove(anyObject());
 verify(map, atLeast(0)).size();
 verify(map, atLeast(0)).entrySet();
 verifyNoMoreInteractions(map);
}

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

@Override
public V put(K key, V value) {
  if (keyValueMap.containsKey(key)) {
    valueKeyMap.remove(keyValueMap.get(key));
  }
  valueKeyMap.put(value, key);
  return keyValueMap.put(key, value);
}

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

@Override
  public void run() {
    try {
      CompletableFuture<Monitor> completableFuture = AbstractMonitorFactory.FUTURES.get(key);
      AbstractMonitorFactory.MONITORS.put(key, completableFuture.get());
      AbstractMonitorFactory.FUTURES.remove(key);
    } catch (InterruptedException e) {
      logger.warn("Thread was interrupted unexpectedly, monitor will never be got.");
      AbstractMonitorFactory.FUTURES.remove(key);
    } catch (ExecutionException e) {
      logger.warn("Create monitor failed, monitor data will not be collected until you fix this problem. ", e);
    }
  }
}

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

@SuppressWarnings("ReferenceEquality")
public Set<String> getInvalidatedClasses(ShadowMap previous) {
 if (this == previous && shadowPickers.isEmpty()) return Collections.emptySet();
 Map<String, ShadowInfo> invalidated = new HashMap<>(overriddenShadows);
 for (Map.Entry<String, ShadowInfo> entry : previous.overriddenShadows.entrySet()) {
  String className = entry.getKey();
  ShadowInfo previousConfig = entry.getValue();
  ShadowInfo currentConfig = invalidated.get(className);
  if (currentConfig == null) {
   invalidated.put(className, previousConfig);
  } else if (previousConfig.equals(currentConfig)) {
   invalidated.remove(className);
  }
 }
 HashSet<String> classNames = new HashSet<>(invalidated.keySet());
 classNames.addAll(shadowPickers.keySet());
 return classNames;
}

相关文章