java.util.TreeMap.get()方法的使用及代码示例

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

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

TreeMap.get介绍

[英]Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

More formally, if this map contains a mapping from a key k to a value v such that key compares equal to k according to the map's ordering, then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

A return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The #containsKey operation may be used to distinguish these two cases.
[中]返回指定键映射到的值,如果此映射不包含该键的映射,则返回null。
更正式地说,如果此映射包含从键k到值v的映射,使得键根据映射的顺序比较等于k,则此方法返回v;否则返回null。(最多可以有一个这样的映射。)
null的返回值不一定表示映射不包含键的映射;映射也可能显式地将密钥映射为null。#containsKey操作可用于区分这两种情况。

代码示例

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

@Override
public void add(Integer value) {
  Integer current = treeMap.get(value);
  Integer newValue = (current != null ? current : 0) + 1;
  this.treeMap.put(value, newValue);
}

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

/**
 * Splits the range set at the given timestamp (if it hasn't been split yet)
 */
private void splitAt(long t) {
  if (data.containsKey(t)) return; // already split at this timestamp
  SortedMap<Long, int[]> head = data.headMap(t);
  int v = head.isEmpty() ? 0 : data.get(head.lastKey())[0];
  data.put(t, new int[]{v});
}

代码示例来源:origin: hankcs/HanLP

/**
 * 将分数map排序折叠
 * @param scoreMap
 * @return
 */
private static TreeMap<Double ,Set<String>> sortScoreMap(TreeMap<String, Double> scoreMap)
{
  TreeMap<Double, Set<String>> result = new TreeMap<Double, Set<String>>(Collections.reverseOrder());
  for (Map.Entry<String, Double> entry : scoreMap.entrySet())
  {
    Set<String> sentenceSet = result.get(entry.getValue());
    if (sentenceSet == null)
    {
      sentenceSet = new HashSet<String>();
      result.put(entry.getValue(), sentenceSet);
    }
    sentenceSet.add(entry.getKey());
  }
  return result;
}

代码示例来源:origin: hankcs/HanLP

@Override
public List<String> suggest(String key, int size)
{
  List<String> resultList = new ArrayList<String>(size);
  TreeMap<String, Double> scoreMap = new TreeMap<String, Double>();
  for (BaseScorer scorer : scorerList)
  {
    Map<String, Double> map = scorer.computeScore(key);
    Double max = max(map);  // 用于正规化一个map
    for (Map.Entry<String, Double> entry : map.entrySet())
    {
      Double score = scoreMap.get(entry.getKey());
      if (score == null) score = 0.0;
      scoreMap.put(entry.getKey(), score / max + entry.getValue() * scorer.boost);
    }
  }
  for (Map.Entry<Double, Set<String>> entry : sortScoreMap(scoreMap).entrySet())
  {
    for (String sentence : entry.getValue())
    {
      if (resultList.size() >= size) return resultList;
      resultList.add(sentence);
    }
  }
  return resultList;
}

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

classMap.put(cls.getName(), cls);
Optional<String> testedClassName = TEST_SUFFIX.chop(cls.getName());
if (testedClassName.isPresent()) {
 Class<?> testedClass = classMap.get(testedClassName.get());
 if (testedClass != null) {
  testClasses.put(testedClass, cls);
result.add(candidate);

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

String[] desc = tags.remove(prevLn);
              out.write(desc[2]);
              out.write(" </i>");
          hit.setLine(sb.toString());
          if (prevHi) {
           String[] desc = tags.remove(prevLn);
           hit.setTag(desc[2]);
          hits.add(hit);
for(Integer rem : tags.keySet()) {
  String[] desc = tags.get(rem);
  out.write("<a class=\"s\" href=\"");
  out.write(url);
for(Integer rem : tags.keySet()) {
  String[] desc = tags.get(rem);
  hit = new Hit(url, "<html>" + Util.htmlize(desc[3]).replace(desc[0], "<b>" + desc[0] + "</b>"),
         desc[1], false, alt);
  hit.setTag(desc[2]);
  hits.add(hit);

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

TreeMap<String, String> myMap = new TreeMap<String, String>();
String first = myMap.firstEntry().getValue();
String firstOther = myMap.get(myMap.firstKey());

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

public void resetLogLevels() {
  TreeMap<String, LogLevel> latestLogLevelMap = latestLogConfig.get();
  LOG.debug("Resetting log levels: Latest log config is {}", latestLogLevelMap);
  LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
  for (String loggerName : latestLogLevelMap.descendingKeySet()) {
    LogLevel loggerSetting = latestLogLevelMap.get(loggerName);
    long timeout = loggerSetting.get_reset_log_level_timeout_epoch();
    String resetLogLevel = loggerSetting.get_reset_log_level();
    if (timeout < Time.currentTimeMillis()) {
      LOG.info("{}: Resetting level to {}", loggerName, resetLogLevel);
      setLoggerLevel(loggerContext, loggerName, resetLogLevel);
      latestLogConfig.getAndUpdate(input -> {
        TreeMap<String, LogLevel> result = new TreeMap<>(input);
        result.remove(loggerName);
        return result;
      });
    }
  }
  loggerContext.updateLoggers();
}

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

private static boolean removeFromRunningTaskMap(TreeMap<Integer, TreeSet<TaskInfo>> runningTasks,
  Object task, TaskInfo taskInfo) {
 int priority = taskInfo.priority.getPriority();
 Set<TaskInfo> tasksAtPriority = runningTasks.get(priority);
 if (tasksAtPriority == null) return false;
 boolean result = tasksAtPriority.remove(taskInfo);
 if (tasksAtPriority.isEmpty()) {
  runningTasks.remove(priority);
 }
 return result;
}

代码示例来源:origin: sannies/mp4parser

public V get(Object k) {
  if (!(k instanceof Comparable)) {
    return null;
  }
  Comparable<K> key = (Comparable<K>) k;
  if (isEmpty()) {
    return null;
  }
  Iterator<K> keys = base.keySet().iterator();
  K a = keys.next();
  do {
    if (keys.hasNext()) {
      if (key.compareTo(a) < 0) {
        a = keys.next();
      } else {
        return base.get(a);
      }
    } else {
      return base.get(a);
    }
  } while (true);
}

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

@Override
public void execute(BatchInfo info, Tuple input) {
  // there won't be a BatchInfo for the success stream
  TransactionAttempt attempt = (TransactionAttempt) input.getValue(0);
  if (input.getSourceStreamId().equals(MasterBatchCoordinator.COMMIT_STREAM_ID)) {
    if (attempt.equals(_activeBatches.get(attempt.getTransactionId()))) {
      ((ICommitterTridentSpout.Emitter) _emitter).commit(attempt);
      _activeBatches.remove(attempt.getTransactionId());
    } else {
      throw new FailedException("Received commit for different transaction attempt");
    }
  } else if (input.getSourceStreamId().equals(MasterBatchCoordinator.SUCCESS_STREAM_ID)) {
    // valid to delete before what's been committed since 
    // those batches will never be accessed again
    _activeBatches.headMap(attempt.getTransactionId()).clear();
    _emitter.success(attempt);
  } else {
    _collector.setBatch(info.batchId);
    _emitter.emitBatch(attempt, input.getValue(1), _collector);
    _activeBatches.put(attempt.getTransactionId(), attempt);
  }
}

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

/**
 * Returns null if it was created, the value otherwise.
 */
public Object getStateOrCreate(long txid, StateInitializer init) {
  Object state;
  if (_curr.containsKey(txid)) {
    state = _curr.get(txid);
  } else {
    getState(txid, init);
    state = null;
  }
  return state;
}

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

@Override
public PartitionHolder<ObjectType> findEntry(Interval interval, VersionType version)
{
 try {
  lock.readLock().lock();
  for (Map.Entry<Interval, TreeMap<VersionType, TimelineEntry>> entry : allTimelineEntries.entrySet()) {
   if (entry.getKey().equals(interval) || entry.getKey().contains(interval)) {
    TimelineEntry foundEntry = entry.getValue().get(version);
    if (foundEntry != null) {
     return new ImmutablePartitionHolder<ObjectType>(
       foundEntry.getPartitionHolder()
     );
    }
   }
  }
  return null;
 }
 finally {
  lock.readLock().unlock();
 }
}

代码示例来源:origin: hankcs/HanLP

public boolean load(InputStream inputStream)
  TreeMap<String, Set<Long>> treeMap = new TreeMap<String, Set<Long>>();
  String line = null;
  try
      for (Synonym synonym : synonymList)
        Set<Long> idSet = treeMap.get(synonym.realWord);
        if (idSet == null)
          treeMap.put(synonym.realWord, idSet);
    for (String key : treeMap.keySet())
      keyList.add(key);
      valueList.add(idSet.toArray(new Long[0]));

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

private void addPendingTask(TaskInfo taskInfo) {
 writeLock.lock();
 try {
  List<TaskInfo> tasksAtPriority = pendingTasks.get(taskInfo.priority);
  if (tasksAtPriority == null) {
   tasksAtPriority = new LinkedList<>();
   pendingTasks.put(taskInfo.priority, tasksAtPriority);
  }
  // Delayed tasks will not kick in right now. That will happen in the scheduling loop.
  tasksAtPriority.add(taskInfo);
  knownTasks.putIfAbsent(taskInfo.task, taskInfo);
  tasksById.put(taskInfo.attemptId, taskInfo);
  if (metrics != null) {
   metrics.incrPendingTasksCount();
  }
  if (LOG.isInfoEnabled()) {
   LOG.info("PendingTasksInfo={}", constructPendingTaskCountsLogMessage());
  }
 } finally {
  writeLock.unlock();
 }
}

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

private TreeMap<Long, Integer> getStoredCurrAttempts(long currTransaction, int maxBatches) {
  TreeMap<Long, Integer> ret = new TreeMap<Long, Integer>();
  for (TransactionalState state : _states) {
    Map<Object, Number> attempts = (Map) state.getData(CURRENT_ATTEMPTS);
    if (attempts == null) {
      attempts = new HashMap();
    }
    for (Entry<Object, Number> e : attempts.entrySet()) {
      // this is because json doesn't allow numbers as keys...
      // TODO: replace json with a better form of encoding
      Number txidObj;
      if (e.getKey() instanceof String) {
        txidObj = Long.parseLong((String) e.getKey());
      } else {
        txidObj = (Number) e.getKey();
      }
      long txid = ((Number) txidObj).longValue();
      int attemptId = ((Number) e.getValue()).intValue();
      Integer curr = ret.get(txid);
      if (curr == null || attemptId > curr) {
        ret.put(txid, attemptId);
      }
    }
  }
  ret.headMap(currTransaction).clear();
  ret.tailMap(currTransaction + maxBatches - 1).clear();
  return ret;
}

代码示例来源:origin: hankcs/HanLP

static void combineChain(TreeMap<String, String> s2t, TreeMap<String, String> t2x)
{
  for (Map.Entry<String, String> entry : s2t.entrySet())
  {
    String x = t2x.get(entry.getValue());
    if (x != null)
    {
      entry.setValue(x);
    }
  }
  for (Map.Entry<String, String> entry : t2x.entrySet())
  {
    String s = CharTable.convert(entry.getKey());
    if (!s2t.containsKey(s))
    {
      s2t.put(s, entry.getValue());
    }
  }
}

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

private void handleManifestExpiredMessage(long eventTimeUs, long manifestPublishTimeMsInEmsg) {
 Long previousExpiryTimeUs = manifestPublishTimeToExpiryTimeUs.get(manifestPublishTimeMsInEmsg);
 if (previousExpiryTimeUs == null) {
  manifestPublishTimeToExpiryTimeUs.put(manifestPublishTimeMsInEmsg, eventTimeUs);
 } else {
  if (previousExpiryTimeUs > eventTimeUs) {
   manifestPublishTimeToExpiryTimeUs.put(manifestPublishTimeMsInEmsg, eventTimeUs);
  }
 }
}

代码示例来源:origin: FudanNLP/fnlp

private void deleteSortMap(int a, int b, double oridata) {
  Set<String> set = sortmap.get(oridata);
  if (set == null)
    return;
  if (set.size() == 1)
    sortmap.remove(oridata);
  else
    set.remove(id2String(a, b));
}

代码示例来源:origin: com.h2database/h2

@Override
public void truncate(long size) {
  writeCount.incrementAndGet();
  if (size == 0) {
    fileSize = 0;
    memory.clear();
    return;
  }
  fileSize = size;
  for (Iterator<Long> it = memory.keySet().iterator(); it.hasNext();) {
    long pos = it.next();
    if (pos < size) {
      break;
    }
    ByteBuffer buff = memory.get(pos);
    if (buff.capacity() > size) {
      throw DataUtils.newIllegalStateException(
          DataUtils.ERROR_READING_FAILED,
          "Could not truncate to {0}; " +
          "partial truncate is not supported", pos);
    }
    it.remove();
  }
}

相关文章