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

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

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

Collection.remove介绍

[英]Removes one instance of the specified object from this Collection if one is contained (optional). The element elem that is removed complies with (object==null ? elem==null : object.equals(elem).
[中]

代码示例

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

/**
   * Update disabled data source names.
   * 
   * @param dataSourceName data source name
   * @param isDisabled is disabled
   */
  public void updateDisabledDataSourceNames(final String dataSourceName, final boolean isDisabled) {
    if (isDisabled) {
      disabledDataSourceNames.add(dataSourceName);
    } else {
      disabledDataSourceNames.remove(dataSourceName);
    }
  }
}

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

@Override
public boolean retainAll(final Collection<?> collection) {
 boolean changed = false;
 Iterator<E> itr = unfiltered.iterator();
 while (itr.hasNext()) {
  E e = itr.next();
  if (predicate.apply(e) && !collection.contains(e)) {
   itr.remove();
   changed = true;
  }
 }
 return changed;
}

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

/**
 * Remove table from the group.
 *
 * @param tbl Table.
 * @return If group is empty after removal.
 */
public boolean removeTable(PartitionTable tbl) {
  tbls.remove(tbl);
  return tbls.isEmpty();
}

代码示例来源:origin: jenkinsci/jenkins

private <T> boolean removeComponent(Collection<ExtensionComponent<T>> collection, Object t) {
  for (Iterator<ExtensionComponent<T>> itr = collection.iterator(); itr.hasNext();) {
    ExtensionComponent<T> c =  itr.next();
    if (c.getInstance().equals(t)) {
      return collection.remove(c);
    }
  }
  return false;
}

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

assert set.add(i);
assert !set.add(i);
assert set.contains(i);
assert set.remove(i);
assert !set.remove(i);
assert !set.contains(i);
assert set.isEmpty();
for (Iterator<Integer> iter = set.iterator(); iter.hasNext(); cnt++)
  c1.contains(iter.next());
assert set.isEmpty();
  set.iterator().next();
  set.add(null);

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

@Override
public LogObserver removeObserver(final String observerIdentifier) {
  writeLock.lock();
  try {
    final LogObserver observer = observerLookup.get(observerIdentifier);
    for (final Collection<LogObserver> collection : observers.values()) {
      collection.remove(observer);
    }
    return observerLookup.remove(observerIdentifier);
  } finally {
    writeLock.unlock();
  }
}

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

public void remove(K k, V v) {
  Collection<V> s = map.get(k);
  if (s != null) {
    s.remove(v);
    if (s.isEmpty()) {
      map.remove(k);
    }
  }
}

代码示例来源:origin: commons-collections/commons-collections

assertEquals(m.isEmpty(), s.isEmpty());
  assertTrue(s.contains(new LocalTestNode(k)));
assertTrue(s.contains(node));
for (Iterator iter = s.iterator(); iter.hasNext(); ) {
  iter.next();
Iterator iter = s.iterator();
  iter.next();
  fail("next() should have thrown an exception after a put");
} catch (ConcurrentModificationException ignored) {}
iter = s.iterator();
  iter.next();
  fail("next() should have thrown an exception after a Map remove");
} catch (ConcurrentModificationException ignored) {}
s.remove(node);
while (iter2.hasNext()) {
for (iter = s.iterator(); iter.hasNext(); ) {

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

/**
 *
 * @see org.springframework.security.acls.afterinvocation.Filterer#getFilteredObject()
 */
public Object getFilteredObject() {
  // Now the Iterator has ended, remove Objects from Collection
  Iterator<T> removeIter = removeList.iterator();
  int originalSize = collection.size();
  while (removeIter.hasNext()) {
    collection.remove(removeIter.next());
  }
  if (logger.isDebugEnabled()) {
    logger.debug("Original collection contained " + originalSize
        + " elements; now contains " + collection.size() + " elements");
  }
  return collection;
}

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

public void testValuesRemove() {
 final Map<K, V> map;
 try {
  map = makePopulatedMap();
 } catch (UnsupportedOperationException e) {
  return;
 }
 Collection<V> valueCollection = map.values();
 if (supportsRemove) {
  int initialSize = map.size();
  valueCollection.remove(valueCollection.iterator().next());
  assertEquals(initialSize - 1, map.size());
  // (We can't assert that the values collection no longer contains the
  // removed value, because the underlying map can have multiple mappings
  // to the same value.)
 } else {
  try {
   valueCollection.remove(valueCollection.iterator().next());
   fail("Expected UnsupportedOperationException.");
  } catch (UnsupportedOperationException expected) {
  }
 }
 assertInvariants(map);
}

代码示例来源:origin: jenkinsci/jenkins

private Map<Job,Collection<ReverseBuildTrigger>> calculateCache() {
  try (ACLContext acl = ACL.as(ACL.SYSTEM)) {
    final Map<Job, Collection<ReverseBuildTrigger>> result = new WeakHashMap<>();
    for (Job<?, ?> downstream : Jenkins.getInstance().allItems(Job.class)) {
      ReverseBuildTrigger trigger =
          ParameterizedJobMixIn.getTrigger(downstream, ReverseBuildTrigger.class);
      if (trigger == null) {
        continue;
      }
      List<Job> upstreams =
          Items.fromNameList(downstream.getParent(), trigger.getUpstreamProjects(), Job.class);
      LOGGER.log(Level.FINE, "from {0} see upstreams {1}", new Object[]{downstream, upstreams});
      for (Job upstream : upstreams) {
        if (upstream instanceof AbstractProject && downstream instanceof AbstractProject) {
          continue; // handled specially
        }
        Collection<ReverseBuildTrigger> triggers = result.get(upstream);
        if (triggers == null) {
          triggers = new LinkedList<>();
          result.put(upstream, triggers);
        }
        triggers.remove(trigger);
        triggers.add(trigger);
      }
    }
    return result;
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

@Override
public void removeMapping(K key, V value) {
 Collection<V> deltaC = deltaMap.get(key);
 if (deltaC == null) {
  Collection<V> originalC = originalMap.get(key);
  if (originalC != null && originalC.contains(value)) {
   deltaC = cf.newCollection();
   deltaC.addAll(originalC);
   deltaMap.put(key, deltaC);
  }
 }
 if (deltaC != null) {
  deltaC.remove(value);
 }
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Removes the value from the Collection mapped to by this key, leaving the
 * rest of the collection intact.
 *
 * @param key The key to the Collection to remove the value from
 * @param value The value to remove
 */
public void removeMapping(K key, V value) {
 if (treatCollectionsAsImmutable) {
  Collection<V> c = map.get(key);
  if (c != null) {
   Collection<V> newC = cf.newCollection();
   newC.addAll(c);
   newC.remove(value);
   map.put(key, newC);
  }
 } else {
  Collection<V> c = get(key);
  c.remove(value);
 }
}

代码示例来源:origin: jankotek/mapdb

/** TODO: more random adders and removers */
static <E> Runnable adderRemover(Collection<E> c, E e) {
  return chooseOne(
    () -> {
      assertTrue(c.add(e));
      assertTrue(c.contains(e));
      assertTrue(c.remove(e));
      assertFalse(c.contains(e));
    },
    () -> {
      assertTrue(c.add(e));
      assertTrue(c.contains(e));
      assertTrue(c.removeIf(x -> x == e));
      assertFalse(c.contains(e));
    },
    () -> {
      assertTrue(c.add(e));
      assertTrue(c.contains(e));
      for (Iterator it = c.iterator();; )
        if (it.next() == e) {
          try { it.remove(); }
          catch (UnsupportedOperationException ok) {
            c.remove(e);
          }
          break;
        }
      assertFalse(c.contains(e));
    });
}

代码示例来源:origin: oracle/helidon

@Override
public String apply(Config.Key key, String stringValue) {
  if (unresolvedKeys.contains(key)) {
    throw new IllegalStateException("Recursive update");
  }
  try {
    unresolvedKeys.add(key);
    return format(stringValue);
  } catch (MissingValueException e) {
    if (failOnMissingReference) {
      throw new ConfigException(String.format(MISSING_REFERENCE_ERROR, key.name()), e);
    } else {
      LOGGER.log(Level.FINER, e, () -> String.format(MISSING_REFERENCE_ERROR, key.name()));
      return stringValue;
    }
  } finally {
    unresolvedKeys.remove(key);
  }
}

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

/** {@inheritDoc} */
@Override protected boolean onPage(UUID nodeId, boolean last) {
  assert Thread.holdsLock(this);
  if (!loc) {
    rcvd.add(nodeId);
    if (rcvd.containsAll(subgrid))
      firstPageLatch.countDown();
  }
  boolean futFinish;
  if (last) {
    futFinish = loc || (subgrid.remove(nodeId) && subgrid.isEmpty());
    if (futFinish)
      firstPageLatch.countDown();
  }
  else
    futFinish = false;
  return futFinish;
}

代码示例来源:origin: TooTallNate/Java-WebSocket

/**
 * This method performs remove operations on the connection and therefore also gives control over whether the operation shall be synchronized
 * <p>
 * {@link #WebSocketServer(InetSocketAddress, int, List, Collection)} allows to specify a collection which will be used to store current connections in.<br>
 * Depending on the type on the connection, modifications of that collection may have to be synchronized.
 * @param ws The Websocket connection which should be removed
 * @return Removing connection successful
 */
protected boolean removeConnection( WebSocket ws ) {
  boolean removed = false;
  synchronized ( connections ) {
    if (this.connections.contains( ws )) {
      removed = this.connections.remove( ws );
    } else {
      //Don't throw an assert error if the ws is not in the list. e.g. when the other endpoint did not send any handshake. see #512
      log.trace("Removing connection which is not in the connections collection! Possible no handshake recieved! {}", ws);
    }
  }
  if( isclosed.get() && connections.isEmpty() ) {
    selectorthread.interrupt();
  }
  return removed;
}

代码示例来源:origin: stackoverflow.com

Collection<String> list = new ArrayList(Arrays.asList("a","b", "c", "d", "e", "f", "g"));
 List<String> sourceList = new ArrayList<String>(list);
 List<String> destinationList = new ArrayList<String>(list);
 list.add("boo");
 list.remove("b");
 sourceList.removeAll( list );
 list.removeAll( destinationList );
 System.out.println( sourceList );
 System.out.println( list );

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

/**
 * Removes a node and all of its replicas.
 *
 * @param node Node to remove (if {@code null}, then no-op).
 * @return {@code True} if node was removed, {@code false} if node is {@code null} or
 *      not present in hash.
 */
public boolean removeNode(N node) {
  if (node == null)
    return false;
  rw.writeLock().lock();
  try {
    if (!nodes.remove(node))
      return false;
    for (Iterator<SortedSet<N>> it = circle.values().iterator(); it.hasNext();) {
      SortedSet<N> set = it.next();
      if (!set.remove(node))
        continue;
      if (set.isEmpty())
        it.remove();
    }
    return true;
  }
  finally {
    rw.writeLock().unlock();
  }
}

代码示例来源:origin: commons-collections/commons-collections

for (Iterator iter = m.values().iterator(); iter.hasNext(); ) {
  iter.next();
  iter.remove();
  assertTrue(s.remove(nodes[k]));
  assertTrue(!s.contains(nodes[k]));
  assertTrue(!m.containsKey(nodes[k].getKey()));
  assertTrue(!m.containsValue(nodes[k]));
Collection c2 = new LinkedList();
c2.add(new LocalTestNode(-123));
  c1.add(nodes[k]);
  c2.add(nodes[k]);

相关文章