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

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

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

Collections.checkedMap介绍

[英]Returns a dynamically typesafe view of the specified map. Trying to insert an element of the wrong type into this map throws a ClassCastException. At creation time the types in m are not checked for correct type.
[中]返回指定映射的动态类型安全视图。试图在此映射中插入错误类型的元素会引发ClassCastException。在创建时,没有检查m中的类型是否正确。

代码示例

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

@Override
 protected Map<String, String> create(Entry<String, String>[] entries) {
  Map<String, String> map = populate(new HashMap<String, String>(), entries);
  return Collections.checkedMap(map, String.class, String.class);
 }
})

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

LinkedProperties() {
  this(Collections.checkedMap(new LinkedHashMap<String, String>(), String.class, String.class));
}

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

@SuppressWarnings("CloneDoesntCallSuperClone")
  public LinkedProperties clone() {
    return new LinkedProperties(Collections.checkedMap(new LinkedHashMap<String, String>(realMap), String.class, String.class));
  }
}

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

classes.add(Collections.checkedList(randomAccessList, Void.class).getClass());
classes.add(Collections.checkedList(nonRandomAccessList, Void.class).getClass());
classes.add(Collections.checkedMap(Collections.emptyMap(), Void.class, Void.class).getClass());
classes.add(Collections.checkedNavigableMap(Collections.emptyNavigableMap(), Void.class, Void.class).getClass());
classes.add(Collections.checkedNavigableSet(Collections.emptyNavigableSet(), Void.class).getClass());

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

PojoWithObjectMapFields fill()
{
  TreeMap<String, String> tm = new TreeMap<String, String>();
  tm.put("foo", "bar");
  EnumMap<GuitarPickup, Size> em = new EnumMap<GuitarPickup, Size>(
      GuitarPickup.class);
  em.put(GuitarPickup.CONTACT, Size.SMALL);
  emptyMap = Collections.emptyMap();
  singletonMap = Collections.singletonMap("key", "value");
  unmodifiableMap = Collections.unmodifiableMap(Collections
      .emptyMap());
  unmodifiableSortedMap = Collections.unmodifiableSortedMap(tm);
  synchronizedMap = Collections.synchronizedMap(em);
  synchronizedSortedMap = Collections.synchronizedSortedMap(tm);
  checkedMap = Collections.checkedMap(em, GuitarPickup.class,
      Size.class);
  checkedSortedMap = Collections.checkedSortedMap(tm, String.class,
      String.class);
  return this;
}

代码示例来源:origin: org.bitbucket.dollar/dollar

@Override
public Dollar.MapWrapper<K, V> checked(Class<K> requiredKeyClass, Class<V> requiredValueClass) {
  map = Collections.checkedMap(map, requiredKeyClass, requiredValueClass);
  return this;
}

代码示例来源:origin: com.google.guava/guava-testlib

@Override
 protected Map<String, String> create(Entry<String, String>[] entries) {
  Map<String, String> map = populate(new HashMap<String, String>(), entries);
  return Collections.checkedMap(map, String.class, String.class);
 }
})

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

private Map<String, ?> map;

public <V> void createMap(Class<V> clazz) {
  map = Collections.checkedMap(new HashMap<String, V>(),
   String.class, clazz);
}

代码示例来源:origin: eu.rssw.openedge.parsers/proparse

/**
 * Only Scope and derivatives may create a Scope object.
 * 
 * @param parentScope null if called by the SymbolScopeRoot constructor.
 */
@SuppressWarnings({"unchecked", "rawtypes"})
private TreeParserSymbolScope(TreeParserSymbolScope parentScope) {
 this.parentScope = parentScope;
 typeMap.put(ProParserTokenTypes.VARIABLE, Collections.checkedMap((Map) variableMap, String.class, Symbol.class));
}

代码示例来源:origin: biz.aQute.bnd/biz.aQute.bndlib

public static <K, V> Map<K, V> map(Class<K> key, Class<V> value) {
  return Collections.checkedMap(new LinkedHashMap<>(), key, value);
}

代码示例来源:origin: Riverside-Software/sonar-openedge

/**
 * Only Scope and derivatives may create a Scope object.
 * 
 * @param parentScope null if called by the SymbolScopeRoot constructor.
 */
@SuppressWarnings({"unchecked", "rawtypes"})
private TreeParserSymbolScope(TreeParserSymbolScope parentScope) {
 this.parentScope = parentScope;
 typeMap.put(ProParserTokenTypes.VARIABLE, Collections.checkedMap((Map) variableMap, String.class, Symbol.class));
}

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

Class<String> type = String.class;
Map<String, String> hashMap = new HashMap<>();
Map<String, String> map = Collections.checkedMap(hashMap, type, type);

Map rawType = map; // pre-Java 1.5 code knows nothing about generics
rawType.put(1, 2); // throws ClassCastException at runtime

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

Map<String, Object> map = new HashMap<String, Object>();
Map<String, String> newMap = new HashMap<String, String>();
@SuppressWarnings("unchecked") Map<String, Object> intermediate =
  (Map)Collections.checkedMap(newMap, String.class, String.class);
intermediate.putAll(map);

代码示例来源:origin: org.glassfish.common/amx-core

/**
@return a Map of ObjectNames from a Map whose values are AMX.
 */
public static Map<String, ObjectName> toObjectNameMap(final Map<String, ? extends AMXProxy> amxMap) {
  final Map<String, ObjectName> m = new HashMap<String, ObjectName>();
  for (final String key : amxMap.keySet()) {
    final AMXProxy value = amxMap.get(key);
    m.put(key, value.objectName());
  }
  return (Collections.checkedMap(m, String.class, ObjectName.class));
}

代码示例来源:origin: org.glassfish.main.common/amx-core

/**
Create a checked Map<String,String>, first verifying that all keys
and values are in fact String.
@param m the Map
@throws ClassCastException
 */
public static <K, V> Map<K, V> checkedMap(final Map<?, ?> m, final Class<K> keyClass, final Class<V> valueClass)
{
  final Map<K, V> cm = checkMap(m, keyClass, valueClass);
  return Collections.checkedMap(cm, keyClass, valueClass);
}

代码示例来源:origin: org.glassfish.main.common/amx-core

/**
@return a Map of ObjectNames from a Map whose values are AMX.
 */
public static Map<String, ObjectName> toObjectNameMap(final Map<String, ? extends AMXProxy> amxMap) {
  final Map<String, ObjectName> m = new HashMap<String, ObjectName>();
  for (final Map.Entry<String,? extends AMXProxy> e : amxMap.entrySet()) {
    final AMXProxy value = e.getValue();
    m.put(e.getKey(), value.objectName());
  }
  return (Collections.checkedMap(m, String.class, ObjectName.class));
}

代码示例来源:origin: org.jboss.eap/wildfly-client-all

@SuppressWarnings("CloneDoesntCallSuperClone")
  public LinkedProperties clone() {
    return new LinkedProperties(Collections.checkedMap(new LinkedHashMap<String, String>(realMap), String.class, String.class));
  }
}

代码示例来源:origin: org.wildfly.security/wildfly-elytron

@SuppressWarnings("CloneDoesntCallSuperClone")
  public LinkedProperties clone() {
    return new LinkedProperties(Collections.checkedMap(new LinkedHashMap<String, String>(realMap), String.class, String.class));
  }
}

代码示例来源:origin: org.glassfish.common/amx-core

/**
Create a checked Map<String,String>, first verifying that all keys
and values are in fact String.
@param m the Map
@throws ClassCastException
 */
public static <K, V> Map<K, V> checkedMap(final Map<?, ?> m, final Class<K> keyClass, final Class<V> valueClass)
{
  final Map<K, V> cm = checkMap(m, keyClass, valueClass);
  return Collections.checkedMap(cm, keyClass, valueClass);
}

代码示例来源:origin: org.wildfly.security/wildfly-elytron-ssl

@SuppressWarnings("CloneDoesntCallSuperClone")
  public LinkedProperties clone() {
    return new LinkedProperties(Collections.checkedMap(new LinkedHashMap<String, String>(realMap), String.class, String.class));
  }
}

相关文章

微信公众号

最新文章

更多

Collections类方法