java.util.concurrent.ConcurrentHashMap.keySet()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(208)

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

ConcurrentHashMap.keySet介绍

[英]Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which removes the corresponding mapping from this map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

The view's iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.
[中]返回此映射中包含的键的集合视图。集合由映射支持,因此对映射的更改将反映在集合中,反之亦然。该集合支持元素删除,即通过迭代器从该映射中删除相应的映射。移除,设置。移除、移除所有、保留和清除操作。它不支持添加或添加所有操作。
视图的迭代器是一个“弱一致性”迭代器,它永远不会抛出ConcurrentModificationException,并保证在构造迭代器时遍历元素,并且可能(但不保证)反映构造之后的任何修改。

代码示例

代码示例来源:origin: thinkaurelius/titan

public void dumpOpenManagers() {
  int estimatedSize = openManagers.size();
  logger.trace("---- Begin open HBase store manager list ({} managers) ----", estimatedSize);
  for (HBaseStoreManager m : openManagers.keySet()) {
    logger.trace("Manager {} opened at:", m, openManagers.get(m));
  }
  logger.trace("----   End open HBase store manager list ({} managers)  ----", estimatedSize);
}

代码示例来源:origin: sohutv/cachecloud

/**
 * Removes all mappings from this map whose values are zero.
 * <p/>
 * <p>This method is not atomic: the map may be visible in intermediate states, where some
 * of the zero values have been removed and others have not.
 */
public void removeAllZeros() {
  for (K key : map.keySet()) {
    AtomicLong atomic = map.get(key);
    if (atomic != null && atomic.get() == 0L) {
      map.remove(key, atomic);
    }
  }
}

代码示例来源:origin: sohutv/cachecloud

/**
 * Removes all mappings from this map whose values are zero.
 * <p/>
 * <p>This method is not atomic: the map may be visible in intermediate states, where some
 * of the zero values have been removed and others have not.
 */
public void removeAllZeros() {
  for (K key : map.keySet()) {
    AtomicLong atomic = map.get(key);
    if (atomic != null && atomic.get() == 0L) {
      map.remove(key, atomic);
    }
  }
}

代码示例来源:origin: alibaba/nacos

public String getTaskInfos() {
  StringBuilder sb = new StringBuilder();
  for (String taskType : this.taskProcessors.keySet()) {
    sb.append(taskType).append(":");
    AbstractTask task = this.tasks.get(taskType);
    if (task != null) {
      sb.append(new Date(task.getLastProcessTime()).toString());
    } else {
      sb.append("finished");
    }
    sb.append(Constants.NACOS_LINE_SEPARATOR);
  }
  return sb.toString();
}

代码示例来源:origin: confluentinc/ksql

void completeFuturesUpToAndIncludingSequenceNumber(final long seqNum) {
  synchronized (this) {
   lastCompletedSequenceNumber = seqNum;
  }
  sequenceNumberFutures.keySet().stream()
    .filter(k -> k <= seqNum)
    .forEach(k -> {
     sequenceNumberFutures.get(k).complete(null);
     sequenceNumberFutures.remove(k);
    });
 }
}

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

ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<String, Integer>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);

for (String key : map.keySet()) {
  System.out.println(key + " " + map.get(key));
}

for (Map.Entry<String, Integer> entry : map.entrySet()) {
  String key = entry.getKey().toString();
  Integer value = entry.getValue();
  System.out.println("key, " + key + " value " + value);
}

代码示例来源:origin: alibaba/Tangram-Android

private void renderView(BaseCell cell, View view) {
  if (view instanceof ITangramViewLifeCycle) {
    return;
  }
  if (methodMap.get(cell) == null) {
    return;
  }
  for (Method method : methodMap.get(cell).keySet()) {
    try {
      method.invoke(view, methodMap.get(cell).get(method));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

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

/**
 * Looks up a CatalogInfo by class and name
 *
 * @param clazz
 * @param id
 * @return
 */
public <U extends CatalogInfo> U findByName(Name name, Class<U> clazz) {
  for (Class<T> key : nameMultiMap.keySet()) {
    if (clazz.isAssignableFrom(key)) {
      Map<Name, T> valueMap = nameMultiMap.get(key);
      if (valueMap != null) {
        T t = valueMap.get(name);
        if (t != null) {
          return (U) t;
        }
      }
    }
  }
  return null;
}

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

/**
 * Looks up a CatalogInfo by class and identifier
 *
 * @param id
 * @param clazz
 * @return
 */
public <U extends CatalogInfo> U findById(String id, Class<U> clazz) {
  for (Class<T> key : idMultiMap.keySet()) {
    if (clazz.isAssignableFrom(key)) {
      Map<String, T> valueMap = idMultiMap.get(key);
      if (valueMap != null) {
        T t = valueMap.get(id);
        if (t != null) {
          return (U) t;
        }
      }
    }
  }
  return null;
}

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

public void dump(PrintWriter pwriter) {
  pwriter.print("Sets (");
  pwriter.print(expiryMap.size());
  pwriter.print(")/(");
  pwriter.print(elemMap.size());
  pwriter.println("):");
  ArrayList<Long> keys = new ArrayList<Long>(expiryMap.keySet());
  Collections.sort(keys);
  for (long time : keys) {
    Set<E> set = expiryMap.get(time);
    if (set != null) {
      pwriter.print(set.size());
      pwriter.print(" expire at ");
      pwriter.print(Time.elapsedTimeToDate(time));
      pwriter.println(":");
      for (E elem : set) {
        pwriter.print("\t");
        pwriter.println(elem.toString());
      }
    }
  }
}

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

/**
 * Looks up objects by class and matching predicate.
 *
 * <p>This method is significantly faster than creating a stream and the applying the predicate
 * on it. Just using this approach instead of the stream makes the overall startup of GeoServer
 * with 20k layers go down from 50s to 44s (which is a lot, considering there is a lot of other
 * things going on)
 *
 * @param clazz
 * @param predicate
 * @return
 */
<U extends CatalogInfo> U findFirst(Class<U> clazz, Predicate<U> predicate) {
  for (Class<T> key : nameMultiMap.keySet()) {
    if (clazz.isAssignableFrom(key)) {
      Map<Name, T> valueMap = nameMultiMap.get(key);
      if (valueMap != null) {
        for (T v : valueMap.values()) {
          final U u = (U) v;
          if (predicate == TRUE || predicate.test(u)) {
            return u;
          }
        }
      }
    }
  }
  return null;
}

代码示例来源:origin: alibaba/cobar

/**
 * 终止执行中的通道
 */
private void kill() {
  for (RouteResultsetNode rrn : target.keySet()) {
    Channel c = target.get(rrn);
    if (c != null && c.isRunning()) {
      c.kill();
    }
  }
}

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

/**
 * Looks up objects by class and matching predicate.
 *
 * <p>This method is significantly faster than creating a stream and the applying the predicate
 * on it. Just using this approach instead of the stream makes the overall startup of GeoServer
 * with 20k layers go down from 50s to 44s (which is a lot, considering there is a lot of other
 * things going on)
 *
 * @param clazz
 * @param predicate
 * @return
 */
<U extends CatalogInfo> List<U> list(Class<U> clazz, Predicate<U> predicate) {
  ArrayList<U> result = new ArrayList<U>();
  for (Class<T> key : nameMultiMap.keySet()) {
    if (clazz.isAssignableFrom(key)) {
      Map<Name, T> valueMap = nameMultiMap.get(key);
      if (valueMap != null) {
        for (T v : valueMap.values()) {
          final U u = (U) v;
          if (predicate == TRUE || predicate.test(u)) {
            result.add(u);
          }
        }
      }
    }
  }
  return result;
}

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

/**
 * Remove an exceptions that are older than the current GC version for each member in the RVV.
 */
public void pruneOldExceptions() {
 Set<T> members;
 members = new HashSet<T>(memberToGCVersion.keySet());
 for (T member : members) {
  Long gcVersion = memberToGCVersion.get(member);
  RegionVersionHolder<T> holder;
  holder = memberToVersion.get(member);
  if (holder != null && gcVersion != null) {
   synchronized (holder) {
    holder.removeExceptionsOlderThan(gcVersion);
   }
  }
 }
 localExceptions.removeExceptionsOlderThan(localGCVersion.get());
}

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

public int removeTopicMessages(String topicName) {
  writeLock.lock();
  try {
    int removed = 0;
    Iterator<MessageId> iterator = messageIdPartitionMap.keySet().iterator();
    while (iterator.hasNext()) {
      MessageId messageId = iterator.next();
      if (messageId instanceof TopicMessageIdImpl &&
          ((TopicMessageIdImpl)messageId).getTopicPartitionName().contains(topicName)) {
        ConcurrentOpenHashSet<MessageId> exist = messageIdPartitionMap.get(messageId);
        if (exist != null) {
          exist.remove(messageId);
        }
        iterator.remove();
        removed ++;
      }
    }
    return removed;
  } finally {
    writeLock.unlock();
  }
}

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

public int removeMessagesTill(MessageId msgId) {
  writeLock.lock();
  try {
    int removed = 0;
    Iterator<MessageId> iterator = messageIdPartitionMap.keySet().iterator();
    while (iterator.hasNext()) {
      MessageId messageId = iterator.next();
      if (messageId.compareTo(msgId) <= 0) {
        ConcurrentOpenHashSet<MessageId> exist = messageIdPartitionMap.get(messageId);
        if (exist != null) {
          exist.remove(messageId);
        }
        iterator.remove();
        removed ++;
      }
    }
    return removed;
  } finally {
    writeLock.unlock();
  }
}

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

@LifecycleStop
public void stop()
{
 Preconditions.checkState(started, "SupervisorManager not started");
 synchronized (lock) {
  for (String id : supervisors.keySet()) {
   try {
    supervisors.get(id).lhs.stop(false);
   }
   catch (Exception e) {
    log.warn(e, "Caught exception while stopping supervisor [%s]", id);
   }
  }
  supervisors.clear();
  started = false;
 }
 log.info("SupervisorManager stopped.");
}

代码示例来源:origin: SeldonIO/seldon-server

public List<State> getAllMinHashes(long time)
{
  List<State> states = new ArrayList<>();
  for(Long id : mhcs.keySet())
  {
    int count = mhcs.get(id).getCount(time);
    if (count >= minActivity)
    {
      List<Long> mh = mhcs.get(id).getMinHashes(time);
      if (mh != null && mh.size() > 0)
        states.add(new State(id,mh));
    }
    else if (count == 0)
    {
      mhcs.remove(id);
      //System.out.println("removed "+id);
    }
  }
  System.out.println("Raw number of minHashes "+mhcs.size()+" but will return "+states.size()+" with minActiviy filter at "+minActivity);
  return states;
}

代码示例来源:origin: andpor/react-native-sqlite-storage

/**
 * Clean up and close all open databases.
 */
public void closeAllOpenDatabases() {
  while (!dbrmap.isEmpty()) {
    String dbname = dbrmap.keySet().iterator().next();
    this.closeDatabaseNow(dbname);
    DBRunner r = dbrmap.get(dbname);
    try {
      // stop the db runner thread:
      r.q.put(new DBQuery());
    } catch(Exception ex) {
      FLog.e(TAG, "couldn't stop db thread for db: " + dbname,ex);
    }
    dbrmap.remove(dbname);
  }
}

代码示例来源:origin: andpor/react-native-sqlite-storage

/**
 * Clean up and close all open databases.
 */
public void closeAllOpenDatabases() {
  while (!dbrmap.isEmpty()) {
    String dbname = dbrmap.keySet().iterator().next();
    this.closeDatabaseNow(dbname);
    DBRunner r = dbrmap.get(dbname);
    try {
      // stop the db runner thread:
      r.q.put(new DBQuery());
    } catch(Exception ex) {
      FLog.e(TAG, "couldn't stop db thread for db: " + dbname,ex);
    }
    dbrmap.remove(dbname);
  }
}

相关文章