【问题标题】:Best way to create an empty map in Java在 Java 中创建空地图的最佳方法
【发布时间】:2010-10-12 18:37:30
【问题描述】:

我需要创建一个空地图。

if (fileParameters == null)
    fileParameters = (HashMap<String, String>) Collections.EMPTY_MAP;

问题是上面的代码产生了这个警告: 类型安全:从 Map 到 HashMap 的未经检查的强制转换

创建这个空地图的最佳方法是什么?

【问题讨论】:

  • 你声明的文件参数类型是什么?
  • 你可能也会得到 ClassCastException。
  • fileParameters 应该是 Map 而不是 HashMap。

标签: java dictionary collections hashmap


【解决方案1】:

1) 如果 Map 可以是不可变的:

Collections.emptyMap()

// or, in some cases:
Collections.<String, String>emptyMap()

当编译器无法自动确定需要哪种 Map 时(这称为 type inference),您有时必须使用后者。例如,考虑这样声明的方法:

public void foobar(Map<String, String> map){ ... }

当直接将空 Map 传递给它时,您必须明确类型:

foobar(Collections.emptyMap());                 // doesn't compile
foobar(Collections.<String, String>emptyMap()); // works fine

2)如果您需要能够修改地图,那么例如:

new HashMap<String, String>()

(如tehblanx pointed out


附录:如果您的项目使用Guava,您有以下选择:

1) 不可变映射:

ImmutableMap.of()
// or:
ImmutableMap.<String, String>of()

当然,与Collections.emptyMap() 相比,这里没有什么大的好处。 From the Javadoc:

此映射的行为和性能与 Collections.emptyMap() 相当, 并且主要是为了您的一致性和可维护性 代码。

2) 可以修改的地图:

Maps.newHashMap()
// or:
Maps.<String, String>newHashMap()

Maps 也包含用于实例化其他类型地图的类似工厂方法,例如 TreeMapLinkedHashMap


更新 (2018):在 Java 9 或更高版本中,用于创建不可变空地图的最短代码是:

Map.of()

...使用来自JEP 269 的新convenience factory methods。 ?

【讨论】:

  • 在大多数情况下,类型推断有效(即 map = Collections.emptyMap() 有效)
  • 是的,没错。我将答案编辑得更全面。
【解决方案2】:

【讨论】:

  • 问题是这个映射只能应用于 Map 对象而不是 HashMap
  • 您应该(通常)避免声明其特定类型的对象,而是使用接口(或抽象父对象)。尽量避免“HashMap foo;”并使用“Map foo;”而是
【解决方案3】:
【解决方案4】:

如果需要HashMap的实例,最好的办法是:

fileParameters = new HashMap<String,String>();

由于 Map 是一个接口,如果你想创建一个空实例,你需要选择一些实例化它的类。 HashMap 看起来和其他的一样好 - 所以就用它吧。

【讨论】:

    【解决方案5】:

    Collections.emptyMap(),或者如果类型推断不适用于您的情况,
    Collections.&lt;String, String&gt;emptyMap()

    【讨论】:

      【解决方案6】:

      由于在许多情况下空映射用于 null 安全设计,您可以使用 nullToEmpty 实用方法:

      class MapUtils {
      
        static <K,V> Map<K,V> nullToEmpty(Map<K,V> map) {
          if (map != null) {
            return map;
          } else {
             return Collections.<K,V>emptyMap(); // or guava ImmutableMap.of()
          }
        }
      
      }  
      

      同样适用于集合:

      class SetUtils {
      
        static <T> Set<T> nullToEmpty(Set<T> set) {
          if (set != null) {
            return set;
          } else {
            return Collections.<T>emptySet();
          }
        }
      
      }
      

      和列表:

      class ListUtils {
      
        static <T> List<T> nullToEmpty(List<T> list) {
          if (list != null) {
            return list;
          } else {
            return Collections.<T>emptyList();
          }
        }
      
      }
      

      【讨论】:

        【解决方案7】:

        【讨论】:

        • 您将无法修改以这种方式创建的空地图。
        猜你喜欢
        • 2014-09-06
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 2012-05-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多