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

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

本文整理了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

  1. /**
  2. * Update disabled data source names.
  3. *
  4. * @param dataSourceName data source name
  5. * @param isDisabled is disabled
  6. */
  7. public void updateDisabledDataSourceNames(final String dataSourceName, final boolean isDisabled) {
  8. if (isDisabled) {
  9. disabledDataSourceNames.add(dataSourceName);
  10. } else {
  11. disabledDataSourceNames.remove(dataSourceName);
  12. }
  13. }
  14. }

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

  1. @Override
  2. public boolean retainAll(final Collection<?> collection) {
  3. boolean changed = false;
  4. Iterator<E> itr = unfiltered.iterator();
  5. while (itr.hasNext()) {
  6. E e = itr.next();
  7. if (predicate.apply(e) && !collection.contains(e)) {
  8. itr.remove();
  9. changed = true;
  10. }
  11. }
  12. return changed;
  13. }

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

  1. /**
  2. * Remove table from the group.
  3. *
  4. * @param tbl Table.
  5. * @return If group is empty after removal.
  6. */
  7. public boolean removeTable(PartitionTable tbl) {
  8. tbls.remove(tbl);
  9. return tbls.isEmpty();
  10. }

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

  1. private <T> boolean removeComponent(Collection<ExtensionComponent<T>> collection, Object t) {
  2. for (Iterator<ExtensionComponent<T>> itr = collection.iterator(); itr.hasNext();) {
  3. ExtensionComponent<T> c = itr.next();
  4. if (c.getInstance().equals(t)) {
  5. return collection.remove(c);
  6. }
  7. }
  8. return false;
  9. }

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

  1. assert set.add(i);
  2. assert !set.add(i);
  3. assert set.contains(i);
  4. assert set.remove(i);
  5. assert !set.remove(i);
  6. assert !set.contains(i);
  7. assert set.isEmpty();
  8. for (Iterator<Integer> iter = set.iterator(); iter.hasNext(); cnt++)
  9. c1.contains(iter.next());
  10. assert set.isEmpty();
  11. set.iterator().next();
  12. set.add(null);

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

  1. @Override
  2. public LogObserver removeObserver(final String observerIdentifier) {
  3. writeLock.lock();
  4. try {
  5. final LogObserver observer = observerLookup.get(observerIdentifier);
  6. for (final Collection<LogObserver> collection : observers.values()) {
  7. collection.remove(observer);
  8. }
  9. return observerLookup.remove(observerIdentifier);
  10. } finally {
  11. writeLock.unlock();
  12. }
  13. }

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

  1. public void remove(K k, V v) {
  2. Collection<V> s = map.get(k);
  3. if (s != null) {
  4. s.remove(v);
  5. if (s.isEmpty()) {
  6. map.remove(k);
  7. }
  8. }
  9. }

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

  1. assertEquals(m.isEmpty(), s.isEmpty());
  2. assertTrue(s.contains(new LocalTestNode(k)));
  3. assertTrue(s.contains(node));
  4. for (Iterator iter = s.iterator(); iter.hasNext(); ) {
  5. iter.next();
  6. Iterator iter = s.iterator();
  7. iter.next();
  8. fail("next() should have thrown an exception after a put");
  9. } catch (ConcurrentModificationException ignored) {}
  10. iter = s.iterator();
  11. iter.next();
  12. fail("next() should have thrown an exception after a Map remove");
  13. } catch (ConcurrentModificationException ignored) {}
  14. s.remove(node);
  15. while (iter2.hasNext()) {
  16. for (iter = s.iterator(); iter.hasNext(); ) {

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

  1. /**
  2. *
  3. * @see org.springframework.security.acls.afterinvocation.Filterer#getFilteredObject()
  4. */
  5. public Object getFilteredObject() {
  6. // Now the Iterator has ended, remove Objects from Collection
  7. Iterator<T> removeIter = removeList.iterator();
  8. int originalSize = collection.size();
  9. while (removeIter.hasNext()) {
  10. collection.remove(removeIter.next());
  11. }
  12. if (logger.isDebugEnabled()) {
  13. logger.debug("Original collection contained " + originalSize
  14. + " elements; now contains " + collection.size() + " elements");
  15. }
  16. return collection;
  17. }

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

  1. public void testValuesRemove() {
  2. final Map<K, V> map;
  3. try {
  4. map = makePopulatedMap();
  5. } catch (UnsupportedOperationException e) {
  6. return;
  7. }
  8. Collection<V> valueCollection = map.values();
  9. if (supportsRemove) {
  10. int initialSize = map.size();
  11. valueCollection.remove(valueCollection.iterator().next());
  12. assertEquals(initialSize - 1, map.size());
  13. // (We can't assert that the values collection no longer contains the
  14. // removed value, because the underlying map can have multiple mappings
  15. // to the same value.)
  16. } else {
  17. try {
  18. valueCollection.remove(valueCollection.iterator().next());
  19. fail("Expected UnsupportedOperationException.");
  20. } catch (UnsupportedOperationException expected) {
  21. }
  22. }
  23. assertInvariants(map);
  24. }

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

  1. private Map<Job,Collection<ReverseBuildTrigger>> calculateCache() {
  2. try (ACLContext acl = ACL.as(ACL.SYSTEM)) {
  3. final Map<Job, Collection<ReverseBuildTrigger>> result = new WeakHashMap<>();
  4. for (Job<?, ?> downstream : Jenkins.getInstance().allItems(Job.class)) {
  5. ReverseBuildTrigger trigger =
  6. ParameterizedJobMixIn.getTrigger(downstream, ReverseBuildTrigger.class);
  7. if (trigger == null) {
  8. continue;
  9. }
  10. List<Job> upstreams =
  11. Items.fromNameList(downstream.getParent(), trigger.getUpstreamProjects(), Job.class);
  12. LOGGER.log(Level.FINE, "from {0} see upstreams {1}", new Object[]{downstream, upstreams});
  13. for (Job upstream : upstreams) {
  14. if (upstream instanceof AbstractProject && downstream instanceof AbstractProject) {
  15. continue; // handled specially
  16. }
  17. Collection<ReverseBuildTrigger> triggers = result.get(upstream);
  18. if (triggers == null) {
  19. triggers = new LinkedList<>();
  20. result.put(upstream, triggers);
  21. }
  22. triggers.remove(trigger);
  23. triggers.add(trigger);
  24. }
  25. }
  26. return result;
  27. }
  28. }

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

  1. @Override
  2. public void removeMapping(K key, V value) {
  3. Collection<V> deltaC = deltaMap.get(key);
  4. if (deltaC == null) {
  5. Collection<V> originalC = originalMap.get(key);
  6. if (originalC != null && originalC.contains(value)) {
  7. deltaC = cf.newCollection();
  8. deltaC.addAll(originalC);
  9. deltaMap.put(key, deltaC);
  10. }
  11. }
  12. if (deltaC != null) {
  13. deltaC.remove(value);
  14. }
  15. }

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

  1. /**
  2. * Removes the value from the Collection mapped to by this key, leaving the
  3. * rest of the collection intact.
  4. *
  5. * @param key The key to the Collection to remove the value from
  6. * @param value The value to remove
  7. */
  8. public void removeMapping(K key, V value) {
  9. if (treatCollectionsAsImmutable) {
  10. Collection<V> c = map.get(key);
  11. if (c != null) {
  12. Collection<V> newC = cf.newCollection();
  13. newC.addAll(c);
  14. newC.remove(value);
  15. map.put(key, newC);
  16. }
  17. } else {
  18. Collection<V> c = get(key);
  19. c.remove(value);
  20. }
  21. }

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

  1. /** TODO: more random adders and removers */
  2. static <E> Runnable adderRemover(Collection<E> c, E e) {
  3. return chooseOne(
  4. () -> {
  5. assertTrue(c.add(e));
  6. assertTrue(c.contains(e));
  7. assertTrue(c.remove(e));
  8. assertFalse(c.contains(e));
  9. },
  10. () -> {
  11. assertTrue(c.add(e));
  12. assertTrue(c.contains(e));
  13. assertTrue(c.removeIf(x -> x == e));
  14. assertFalse(c.contains(e));
  15. },
  16. () -> {
  17. assertTrue(c.add(e));
  18. assertTrue(c.contains(e));
  19. for (Iterator it = c.iterator();; )
  20. if (it.next() == e) {
  21. try { it.remove(); }
  22. catch (UnsupportedOperationException ok) {
  23. c.remove(e);
  24. }
  25. break;
  26. }
  27. assertFalse(c.contains(e));
  28. });
  29. }

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

  1. @Override
  2. public String apply(Config.Key key, String stringValue) {
  3. if (unresolvedKeys.contains(key)) {
  4. throw new IllegalStateException("Recursive update");
  5. }
  6. try {
  7. unresolvedKeys.add(key);
  8. return format(stringValue);
  9. } catch (MissingValueException e) {
  10. if (failOnMissingReference) {
  11. throw new ConfigException(String.format(MISSING_REFERENCE_ERROR, key.name()), e);
  12. } else {
  13. LOGGER.log(Level.FINER, e, () -> String.format(MISSING_REFERENCE_ERROR, key.name()));
  14. return stringValue;
  15. }
  16. } finally {
  17. unresolvedKeys.remove(key);
  18. }
  19. }

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

  1. /** {@inheritDoc} */
  2. @Override protected boolean onPage(UUID nodeId, boolean last) {
  3. assert Thread.holdsLock(this);
  4. if (!loc) {
  5. rcvd.add(nodeId);
  6. if (rcvd.containsAll(subgrid))
  7. firstPageLatch.countDown();
  8. }
  9. boolean futFinish;
  10. if (last) {
  11. futFinish = loc || (subgrid.remove(nodeId) && subgrid.isEmpty());
  12. if (futFinish)
  13. firstPageLatch.countDown();
  14. }
  15. else
  16. futFinish = false;
  17. return futFinish;
  18. }

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

  1. /**
  2. * This method performs remove operations on the connection and therefore also gives control over whether the operation shall be synchronized
  3. * <p>
  4. * {@link #WebSocketServer(InetSocketAddress, int, List, Collection)} allows to specify a collection which will be used to store current connections in.<br>
  5. * Depending on the type on the connection, modifications of that collection may have to be synchronized.
  6. * @param ws The Websocket connection which should be removed
  7. * @return Removing connection successful
  8. */
  9. protected boolean removeConnection( WebSocket ws ) {
  10. boolean removed = false;
  11. synchronized ( connections ) {
  12. if (this.connections.contains( ws )) {
  13. removed = this.connections.remove( ws );
  14. } else {
  15. //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
  16. log.trace("Removing connection which is not in the connections collection! Possible no handshake recieved! {}", ws);
  17. }
  18. }
  19. if( isclosed.get() && connections.isEmpty() ) {
  20. selectorthread.interrupt();
  21. }
  22. return removed;
  23. }

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

  1. Collection<String> list = new ArrayList(Arrays.asList("a","b", "c", "d", "e", "f", "g"));
  2. List<String> sourceList = new ArrayList<String>(list);
  3. List<String> destinationList = new ArrayList<String>(list);
  4. list.add("boo");
  5. list.remove("b");
  6. sourceList.removeAll( list );
  7. list.removeAll( destinationList );
  8. System.out.println( sourceList );
  9. System.out.println( list );

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

  1. /**
  2. * Removes a node and all of its replicas.
  3. *
  4. * @param node Node to remove (if {@code null}, then no-op).
  5. * @return {@code True} if node was removed, {@code false} if node is {@code null} or
  6. * not present in hash.
  7. */
  8. public boolean removeNode(N node) {
  9. if (node == null)
  10. return false;
  11. rw.writeLock().lock();
  12. try {
  13. if (!nodes.remove(node))
  14. return false;
  15. for (Iterator<SortedSet<N>> it = circle.values().iterator(); it.hasNext();) {
  16. SortedSet<N> set = it.next();
  17. if (!set.remove(node))
  18. continue;
  19. if (set.isEmpty())
  20. it.remove();
  21. }
  22. return true;
  23. }
  24. finally {
  25. rw.writeLock().unlock();
  26. }
  27. }

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

  1. for (Iterator iter = m.values().iterator(); iter.hasNext(); ) {
  2. iter.next();
  3. iter.remove();
  4. assertTrue(s.remove(nodes[k]));
  5. assertTrue(!s.contains(nodes[k]));
  6. assertTrue(!m.containsKey(nodes[k].getKey()));
  7. assertTrue(!m.containsValue(nodes[k]));
  8. Collection c2 = new LinkedList();
  9. c2.add(new LocalTestNode(-123));
  10. c1.add(nodes[k]);
  11. c2.add(nodes[k]);

相关文章