java.util.Collections.unmodifiableMap()方法的使用及代码示例

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

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

Collections.unmodifiableMap介绍

[英]Returns a wrapper on the specified map which throws an UnsupportedOperationException whenever an attempt is made to modify the map.
[中]返回指定映射上的包装器,每当试图修改映射时,该包装器都会引发UnsupportedOperationException。

代码示例

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

PathMatchInfo(Map<String, String> uriVars,
    @Nullable Map<String, MultiValueMap<String, String>> matrixVars) {
  this.uriVariables = Collections.unmodifiableMap(uriVars);
  this.matrixVariables = matrixVars != null ?
      Collections.unmodifiableMap(matrixVars) : Collections.emptyMap();
}

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

public class Test {
  private static final Map<Integer, String> myMap;
  static {
    Map<Integer, String> aMap = ....;
    aMap.put(1, "one");
    aMap.put(2, "two");
    myMap = Collections.unmodifiableMap(aMap);
  }
}

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

public Set<Map.Entry<String, Object>> entrySet() {
  return Collections.unmodifiableMap(this.headers).entrySet();
}

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

Map<String, String> realMap = new HashMap<String, String>();
realMap.put("A", "B");

Map<String, String> unmodifiableMap = Collections.unmodifiableMap(realMap);

// This is not possible: It would throw an 
// UnsupportedOperationException
//unmodifiableMap.put("C", "D");

// This is still possible:
realMap.put("E", "F");

// The change in the "realMap" is now also visible
// in the "unmodifiableMap". So the unmodifiableMap
// has changed after it has been created.
unmodifiableMap.get("E"); // Will return "F".

代码示例来源:origin: square/okhttp

public Challenge(String scheme, Map<String, String> authParams) {
 if (scheme == null) throw new NullPointerException("scheme == null");
 if (authParams == null) throw new NullPointerException("authParams == null");
 this.scheme = scheme;
 Map<String, String> newAuthParams = new LinkedHashMap<>();
 for (Entry<String, String> authParam : authParams.entrySet()) {
  String key = (authParam.getKey() == null) ? null : authParam.getKey().toLowerCase(US);
  newAuthParams.put(key, authParam.getValue());
 }
 this.authParams = unmodifiableMap(newAuthParams);
}

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

@Override
public JsonWriterFactory createWriterFactory(Map<String, ?> config) {
  Map<String, Object> providerConfig;
  boolean prettyPrinting;
  BufferPool pool;
  if (config == null) {
    providerConfig = Collections.emptyMap();
    prettyPrinting = false;
    pool = bufferPool;
  } else {
    providerConfig = new HashMap<>();
    if (prettyPrinting=JsonProviderImpl.isPrettyPrintingEnabled(config)) {
      providerConfig.put(JsonGenerator.PRETTY_PRINTING, true);
    }
    pool = (BufferPool)config.get(BufferPool.class.getName());
    if (pool != null) {
      providerConfig.put(BufferPool.class.getName(), pool);
    } else {
      pool = bufferPool;
    }
    providerConfig = Collections.unmodifiableMap(providerConfig);
  }
  return new JsonWriterFactoryImpl(providerConfig, prettyPrinting, pool);
}

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

@Override
public Map getParameterMap() {
  if (parameterMap == null) {
    Map<String, String[]> map = new LinkedHashMap<>();
    for (String formParam : formParams.keySet()) {
      map.put(formParam, formParams.get(formParam).toArray(new String[0]));
    }
    map.putAll(super.getParameterMap());
    parameterMap = Collections.unmodifiableMap(map);
  }
  return parameterMap;
}

代码示例来源:origin: cloudfoundry/uaa

public static Map<String, Map<String, Object>> parseYaml(String sampleYaml) {
  YamlMapFactoryBean factory = new YamlMapFactoryBean();
  factory.setResolutionMethod(YamlProcessor.ResolutionMethod.OVERRIDE_AND_IGNORE);
  List<Resource> resources = new ArrayList<>();
  ByteArrayResource resource = new ByteArrayResource(sampleYaml.getBytes());
  resources.add(resource);
  factory.setResources(resources.toArray(new Resource[resources.size()]));
  Map<String, Object> tmpdata = factory.getObject();
  Map<String, Map<String, Object>> dataMap = new HashMap<>();
  for (Map.Entry<String, Object> entry : ((Map<String, Object>)tmpdata.get("providers")).entrySet()) {
    dataMap.put(entry.getKey(), (Map<String, Object>)entry.getValue());
  }
  return Collections.unmodifiableMap(dataMap);
}

代码示例来源:origin: Alluxio/alluxio

@Override
public Map<String, Long> updateValues(Map<MetricsFilter, Set<Metric>> map) {
 Map<String, Long> updated = new HashMap<>();
 for (Metric metric : map.get(mFilter)) {
  Map<String, String> tags = metric.getTags();
  if (tags.containsKey(mTagName)) {
   String ufsName = MetricsSystem.getClusterMetricName(
     Metric.getMetricNameWithTags(mAggregationName, mTagName, tags.get(mTagName)));
   long value = updated.getOrDefault(ufsName, 0L);
   updated.put(ufsName, (long) (value + metric.getValue()));
  }
 }
 synchronized (this) {
  mAggregates = updated;
 }
 return Collections.unmodifiableMap(mAggregates);
}

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

public Map<InjectionTarget, ResourceInjectionConfiguration> getResourceInjections(final String className) {
  Map<InjectionTarget, ResourceInjectionConfiguration> injections = resourceInjections.get(className);
  if (injections == null) {
    return Collections.emptyMap();
  } else {
    return Collections.unmodifiableMap(injections);
  }
}

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

/**
   * @param statType Type of statistics which need to take.
   * @return All tracked statistics for given type.
   */
  public Map<IoStatisticsHolderKey, IoStatisticsHolder> statistics(IoStatisticsType statType){
    return Collections.unmodifiableMap(statByType.get(statType));
  }
}

代码示例来源:origin: skylot/jadx

public static Map<String, String> newConstStringMap(String... parameters) {
    int len = parameters.length;
    if (len == 0) {
      return Collections.emptyMap();
    }
    Map<String, String> result = new HashMap<>(len / 2);
    for (int i = 0; i < len; i += 2) {
      result.put(parameters[i], parameters[i + 1]);
    }
    return Collections.unmodifiableMap(result);
  }
}

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

/**
 * Create a WebSocketExtension with the given name and parameters.
 * @param name the name of the extension
 * @param parameters the parameters
 */
public WebSocketExtension(String name, @Nullable Map<String, String> parameters) {
  Assert.hasLength(name, "Extension name must not be empty");
  this.name = name;
  if (!CollectionUtils.isEmpty(parameters)) {
    Map<String, String> map = new LinkedCaseInsensitiveMap<>(parameters.size(), Locale.ENGLISH);
    map.putAll(parameters);
    this.parameters = Collections.unmodifiableMap(map);
  }
  else {
    this.parameters = Collections.emptyMap();
  }
}

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

/** Returns a map of metadata name, value pairs to be set. Values are copied.  */
public Map<String, Object> getEditedValues() {
 HashMap<String, Object> hashMap = new HashMap<>(editedValues);
 for (Entry<String, Object> entry : hashMap.entrySet()) {
  Object value = entry.getValue();
  if (value instanceof byte[]) {
   byte[] bytes = (byte[]) value;
   entry.setValue(Arrays.copyOf(bytes, bytes.length));
  }
 }
 return Collections.unmodifiableMap(hashMap);
}

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

/**
 * Build a mapping of {@link TypeVariable#getName TypeVariable names} to
 * {@link Class concrete classes} for the specified {@link Class}.
 * Searches all super types, enclosing types and interfaces.
 * @see #resolveType(Type, Map)
 */
@SuppressWarnings("rawtypes")
public static Map<TypeVariable, Type> getTypeVariableMap(Class<?> clazz) {
  Map<TypeVariable, Type> typeVariableMap = typeVariableCache.get(clazz);
  if (typeVariableMap == null) {
    typeVariableMap = new HashMap<>();
    buildTypeVariableMap(ResolvableType.forClass(clazz), typeVariableMap);
    typeVariableCache.put(clazz, Collections.unmodifiableMap(typeVariableMap));
  }
  return typeVariableMap;
}

代码示例来源:origin: square/leakcanary

private Map<String, Exclusion> unmodifiableRefMap(Map<String, ParamsBuilder> fieldBuilderMap) {
 Map<String, Exclusion> fieldMap = new LinkedHashMap<>();
 for (Map.Entry<String, ParamsBuilder> fieldEntry : fieldBuilderMap.entrySet()) {
  fieldMap.put(fieldEntry.getKey(), new Exclusion(fieldEntry.getValue()));
 }
 return unmodifiableMap(fieldMap);
}

代码示例来源:origin: square/okhttp

private static Map<ByteString, Integer> nameToFirstIndex() {
 Map<ByteString, Integer> result = new LinkedHashMap<>(STATIC_HEADER_TABLE.length);
 for (int i = 0; i < STATIC_HEADER_TABLE.length; i++) {
  if (!result.containsKey(STATIC_HEADER_TABLE[i].name)) {
   result.put(STATIC_HEADER_TABLE[i].name, i);
  }
 }
 return Collections.unmodifiableMap(result);
}

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

@Override
public JsonGeneratorFactory createGeneratorFactory(Map<String, ?> config) {
  Map<String, Object> providerConfig;
  boolean prettyPrinting;
  BufferPool pool;
  if (config == null) {
    providerConfig = Collections.emptyMap();
    prettyPrinting = false;
    pool = bufferPool;
  } else {
    providerConfig = new HashMap<>();
    if (prettyPrinting=JsonProviderImpl.isPrettyPrintingEnabled(config)) {
      providerConfig.put(JsonGenerator.PRETTY_PRINTING, true);
    }
    pool = (BufferPool)config.get(BufferPool.class.getName());
    if (pool != null) {
      providerConfig.put(BufferPool.class.getName(), pool);
    } else {
      pool = bufferPool;
    }
    providerConfig = Collections.unmodifiableMap(providerConfig);
  }
  return new JsonGeneratorFactoryImpl(providerConfig, prettyPrinting, pool);
}

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

static Map<String,String> resolveMessagesForOrigin(final Class<?> origin, final Locale locale) {
  final Map<String,String> combinedMessages = new HashMap<String, String>(20);
  Class<?> currentClass = origin;
  combinedMessages.putAll(resolveMessagesForSpecificClass(currentClass, locale));
  while (!currentClass.getSuperclass().equals(Object.class)) {
    currentClass = currentClass.getSuperclass();
    final Map<String,String> messagesForCurrentClass = resolveMessagesForSpecificClass(currentClass, locale);
    for (final String messageKey : messagesForCurrentClass.keySet()) {
      if (!combinedMessages.containsKey(messageKey)) {
        combinedMessages.put(messageKey, messagesForCurrentClass.get(messageKey));
      }
    }
  }
  return Collections.unmodifiableMap(combinedMessages);
}

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

@Override
  public Map<String, String> getProperties(Class<? extends Protocol> protocolClass) {
    Map<String, String> properties = this.map.get(protocolClass);
    return (properties != null) ? Collections.unmodifiableMap(properties) : Collections.<String, String>emptyMap();
  }
}

相关文章

Collections类方法