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

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

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

Map.putIfAbsent介绍

暂无

代码示例

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

/** Add a profile to be returned by {@link #getProfiles(int)}.**/
public void addProfile(
  int userHandle, int profileUserHandle, String profileName, int profileFlags) {
 profiles.putIfAbsent(userHandle, new ArrayList<>());
 profiles.get(userHandle).add(new UserInfo(profileUserHandle, profileName, profileFlags));
}

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

public static void addProtocol(short id, Class protocol) {
  if(id < MIN_CUSTOM_PROTOCOL_ID)
    throw new IllegalArgumentException("protocol ID (" + id + ") needs to be greater than or equal to " + MIN_CUSTOM_PROTOCOL_ID);
  if(protocol_ids.containsKey(protocol))
    alreadyInProtocolsMap(id, protocol.getName());
  protocol_ids.put(protocol, id);
  protocol_names.putIfAbsent(id, protocol);
}

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

throws MissingResourceException {
Map<String, Map<Locale, MessageFormat>> codeMap = this.cachedBundleMessageFormats.get(bundle);
Map<Locale, MessageFormat> localeMap = null;
if (codeMap != null) {
  localeMap = codeMap.get(code);
  if (localeMap != null) {
    MessageFormat result = localeMap.get(locale);
    if (result != null) {
      return result;
    codeMap = new ConcurrentHashMap<>();
    Map<String, Map<Locale, MessageFormat>> existing =
        this.cachedBundleMessageFormats.putIfAbsent(bundle, codeMap);
    if (existing != null) {
      codeMap = existing;
    Map<Locale, MessageFormat> existing = codeMap.putIfAbsent(code, localeMap);
    if (existing != null) {
      localeMap = existing;
  localeMap.put(locale, result);
  return result;

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

RegionInfo currRegion = entry.getKey();
 if (rsGroupInfo.getTables().contains(currTable)) {
  assignments.putIfAbsent(currTable, new HashMap<>());
  assignments.get(currTable).putIfAbsent(currServer, new ArrayList<>());
  assignments.get(currTable).get(currServer).add(currRegion);
for(ServerName serverName: master.getServerManager().getOnlineServers().keySet()) {
 if(rsGroupInfo.getServers().contains(serverName.getAddress())) {
  serverMap.put(serverName, Collections.emptyList());
  result.put(tableName, new HashMap<>());
  result.get(tableName).putAll(serverMap);
  result.get(tableName).putAll(assignments.get(tableName));

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

private Pair<Map<Interval, String>, Map<Interval, List<DataSegment>>> getMaxCreateDateAndBaseSegments(
  List<Pair<DataSegment, String>> snapshot
)
{
 Interval maxAllowedToBuildInterval = snapshot.parallelStream()
   .map(pair -> pair.lhs)
   .map(DataSegment::getInterval)
   .max(Comparators.intervalsByStartThenEnd())
   .get();
 Map<Interval, String> maxCreatedDate = new HashMap<>();
 Map<Interval, List<DataSegment>> segments = new HashMap<>();
 for (Pair<DataSegment, String> entry : snapshot) {
  DataSegment segment = entry.lhs;
  String createDate = entry.rhs;
  Interval interval = segment.getInterval();
  if (!hasEnoughLag(interval, maxAllowedToBuildInterval)) {
   continue;
  }
  maxCreatedDate.put(
    interval, 
    DateTimes.max(
      DateTimes.of(createDate), 
      DateTimes.of(maxCreatedDate.getOrDefault(interval, DateTimes.MIN.toString()))
    ).toString()
  );
  segments.putIfAbsent(interval, new ArrayList<>());
  segments.get(interval).add(segment);
 }
 return new Pair<>(maxCreatedDate, segments);
}

代码示例来源:origin: jooby-project/jooby

@Override
public void render(final View view, final Renderer.Context ctx) throws Exception {
 String name = view.name() + suffix;
 Template template = template(name, ctx.charset());
 Map<String, Object> hash = new HashMap<>();
 hash.put("_vname", view.name());
 hash.put("_vpath", template.getName());
 hash.put("xss", xss);
 // Locale:
 Locale locale = (Locale) hash.getOrDefault("locale", ctx.locale());
 hash.putIfAbsent("locale", locale);
 // locals
 Map<String, Object> locals = ctx.locals();
 hash.putAll(locals);
 // model
 hash.putAll(view.model());
 TemplateModel model = new SimpleHash(hash, new FtlWrapper(freemarker.getObjectWrapper()));
 // TODO: remove string writer
 StringWriter writer = new StringWriter();
 // Locale:
 template.setLocale(locale);
 // output
 template.process(model, writer);
 ctx.type(MediaType.html)
   .send(writer.toString());
}

代码示例来源:origin: jooby-project/jooby

@Override
public void render(final View view, final Renderer.Context ctx) throws Exception {
 String vname = view.name();
 TemplateSource source = handlebars.getLoader().sourceAt(vname);
 Template template = handlebars.compile(source);
 Map<String, Object> locals = ctx.locals();
 locals.putIfAbsent("_vname", vname);
 locals.putIfAbsent("_vpath", source.filename());
 // Locale:
 Locale locale = (Locale) locals.getOrDefault("locale", ctx.locale());
 locals.put("locale", locale);
 com.github.jknack.handlebars.Context context = com.github.jknack.handlebars.Context
   .newBuilder(view.model())
   // merge request locals (req+sessions locals)
   .combine(locals)
   .resolver(resolvers)
   .build();
 // rendering it
 ctx.type(MediaType.html)
   .send(template.apply(context));
}

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

private Pair<Map<Interval, String>, Map<Interval, List<DataSegment>>> getVersionAndBaseSegments(
  List<DataSegment> snapshot
)
{
 Map<Interval, String> versions = new HashMap<>();
 Map<Interval, List<DataSegment>> segments = new HashMap<>();
 for (DataSegment segment : snapshot) {
  Interval interval = segment.getInterval();
  versions.put(interval, segment.getVersion());
  segments.putIfAbsent(interval, new ArrayList<>());
  segments.get(interval).add(segment);
 }
 return new Pair<>(versions, segments);
}

代码示例来源:origin: spring-cloud-incubator/spring-cloud-alibaba

public ConsumerGroupInstrumentation getConsumerGroupInstrumentation(String group) {
  String key = Consumer.GROUP_PREFIX + group;
  consumerGroupsInstrumentations.putIfAbsent(key,
      new ConsumerGroupInstrumentation(metricRegistry, key));
  return consumerGroupsInstrumentations.get(key);
}

代码示例来源:origin: prestodb/presto

private synchronized Map<String, ConnectorId> getCatalogNames()
{
  // todo if repeatable read, this must be recorded
  Map<String, ConnectorId> catalogNames = new HashMap<>();
  catalogByName.values().stream()
      .filter(Optional::isPresent)
      .map(Optional::get)
      .forEach(catalog -> catalogNames.put(catalog.getCatalogName(), catalog.getConnectorId()));
  catalogManager.getCatalogs().stream()
      .forEach(catalog -> catalogNames.putIfAbsent(catalog.getCatalogName(), catalog.getConnectorId()));
  return ImmutableMap.copyOf(catalogNames);
}

代码示例来源:origin: languagetool-org/languagetool

/**
 * Associate a notation with a given unit.
 * @param pattern Regex for recognizing the unit. Word boundaries and numbers are added to this pattern by addUnit itself.
 * @param base Unit to associate with the pattern
 * @param symbol Suffix used for suggestion.
 * @param factor Convenience parameter for prefixes for metric units, unit is multiplied with this. Defaults to 1 if not used.
 * @param metric Register this notation for suggestion.
 */
protected void addUnit(String pattern, Unit base, String symbol, double factor, boolean metric) {
 Unit unit = base.multiply(factor);
 unitPatterns.put(Pattern.compile("\\b" + NUMBER_REGEX + "\\s*" + pattern + "\\b"), unit);
 unitSymbols.putIfAbsent(unit, new ArrayList<>());
 unitSymbols.get(unit).add(symbol);
 if (metric && !metricUnits.contains(unit)) {
  metricUnits.add(unit);
 }
}

代码示例来源:origin: spring-cloud-incubator/spring-cloud-alibaba

public ProducerInstrumentation getProducerInstrumentation(String destination) {
  String key = Producer.PREFIX + destination;
  producerInstrumentations.putIfAbsent(key,
      new ProducerInstrumentation(metricRegistry, key));
  return producerInstrumentations.get(key);
}

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

/**
 * Augment the existing config with new settings, ignoring any conflicting settings.
 *
 * @param setting settings to add and override
 * @throws InvalidSettingException when and invalid setting is found and {@link
 * GraphDatabaseSettings#strict_config_validation} is true.
 */
public void augmentDefaults( Setting<?> setting, String value ) throws InvalidSettingException
{
  overriddenDefaults.put( setting.name(), value );
  params.putIfAbsent( setting.name(), value );
}

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

private Set<String> styleForDepth(int depth, boolean inlineHighlight) {
  if (depth < 0) {
    // that's the style when we're outside any node
    return Collections.emptySet();
  } else {
    // Caching reduces the number of sets used by this step of the overlaying routine to
    // only a few. The number is probably blowing up during the actual spans overlaying
    // in StyleContext#recomputePainting
    DEPTH_STYLE_CACHE.putIfAbsent(style, new HashMap<>());
    Map<Integer, Map<Boolean, Set<String>>> depthToStyle = DEPTH_STYLE_CACHE.get(style);
    depthToStyle.putIfAbsent(depth, new HashMap<>());
    Map<Boolean, Set<String>> isInlineToStyle = depthToStyle.get(depth);
    if (isInlineToStyle.containsKey(inlineHighlight)) {
      return isInlineToStyle.get(inlineHighlight);
    }
    Set<String> s = new HashSet<>(style);
    s.add("depth-" + depth);
    if (inlineHighlight) {
      // inline highlight can be used to add boxing around a node if it wouldn't be ugly
      s.add("inline-highlight");
    }
    isInlineToStyle.put(inlineHighlight, s);
    return s;
  }
}

代码示例来源:origin: spring-cloud-incubator/spring-cloud-alibaba

public ConsumerInstrumentation getConsumerInstrumentation(String destination) {
  String key = Consumer.PREFIX + destination;
  consumeInstrumentations.putIfAbsent(key,
      new ConsumerInstrumentation(metricRegistry, key));
  return consumeInstrumentations.get(key);
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
public boolean addCustomEffect(PotionEffect potionEffect, boolean overwrite) {
  PotionEffectType type = potionEffect.getType();
  if (overwrite) {
    customEffects.put(type, potionEffect);
    return true;
  } else {
    return customEffects.putIfAbsent(type, potionEffect) == null;
  }
}

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

Map<Locale, ResourceBundle> localeMap = this.cachedResourceBundles.get(basename);
if (localeMap != null) {
  ResourceBundle bundle = localeMap.get(locale);
  if (bundle != null) {
    return bundle;
  if (localeMap == null) {
    localeMap = new ConcurrentHashMap<>();
    Map<Locale, ResourceBundle> existing = this.cachedResourceBundles.putIfAbsent(basename, localeMap);
    if (existing != null) {
      localeMap = existing;
  localeMap.put(locale, bundle);
  return bundle;

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

/**
 * @param accMap Map to obtain accumulator from.
 * @param cacheId Cache id.
 * @param part Partition number.
 * @return Accumulator.
 */
private AtomicLong accumulator(Map<Integer, Map<Integer, AtomicLong>> accMap, int cacheId, int part) {
  Map<Integer, AtomicLong> cacheAccs = accMap.get(cacheId);
  if (cacheAccs == null) {
    Map<Integer, AtomicLong> cacheAccs0 =
      accMap.putIfAbsent(cacheId, cacheAccs = new ConcurrentHashMap<>());
    if (cacheAccs0 != null)
      cacheAccs = cacheAccs0;
  }
  AtomicLong acc = cacheAccs.get(part);
  if (acc == null) {
    AtomicLong accDelta0 = cacheAccs.putIfAbsent(part, acc = new AtomicLong());
    if (accDelta0 != null)
      acc = accDelta0;
  }
  return acc;
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
public boolean addCustomEffect(PotionEffect potionEffect, boolean overwrite) {
  PotionEffectType type = potionEffect.getType();
  if (overwrite) {
    customEffects.put(type, potionEffect);
    return true;
  } else {
    return customEffects.putIfAbsent(type, potionEffect) == null;
  }
}

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

Mention m = sortedMentions.get(i);
for (String word : getContentWords(m)) {
 wordToMentions.putIfAbsent(word, new ArrayList<>());
 wordToMentions.get(word).add(m);
 List<Mention> withStringMatch = wordToMentions.get(word);
 if (withStringMatch != null) {
  for (Mention match : withStringMatch) {
 mentionToCandidateAntecedents.put(m.mentionID, candidateAntecedents);

相关文章