如何在初始化时定义地图内容?

发布于 2024-10-15 12:55:43 字数 160 浏览 3 评论 0原文

我只是想知道是否可以在初始化时定义地图对象的内容。

例如,可以创建一个数组,如下所示:

new String[] {“apples”, “bananas”, “pears”}

所以,我想知道我们是否可以为地图做类似的事情。

I was just wondering if it is possible to define the contents of a Map Object on initialisation.

For example, an array can be created, as:

new String[] {“apples”, “bananas”, “pears”}

So, I was wondering if there is something similar we can do for maps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玻璃人 2024-10-22 12:55:44

如果您的 Map 在创建后将是不可变的,并且您不介意添加依赖项,Guava 提供了一些不错的流畅语法

Map<K,V> aMap = ImmutableMap.<K,V>builder().put(key0, val0).put(key1,val1).build();

如果您感觉非常具有异国情调,Scala具有与您想要的语法完全相同的语法,并且可以与其他 Java 代码互操作:

val aMap = Map("a"->0, "b"->1)

请注意,Scala 编译器将推断 Map 泛型类型是从 StringInt,基于您放入的内容,尽管您也可以显式指定它。

但是,如果这只是一次性的,我会使用基于初始值设定项的语法。 Guava 库和 Scala 语言都有很多其他值得推荐的地方,但学习一个全新的库/语言可能有点过分了。

If your Map is going to be immutable after creation and you don't mind adding a dependency, Guava offers some nice fluent syntax:

Map<K,V> aMap = ImmutableMap.<K,V>builder().put(key0, val0).put(key1,val1).build();

If you're feeling really exotic, Scala has syntax exactly like what you want and is interoperable with other Java code:

val aMap = Map("a"->0, "b"->1)

Note that the Scala compiler will infer the Map generic type is from String to Int, based on what you put in it, though you can explicitly specify it as well.

However, if this is just a one-off, I'd go with the initializer-based syntax. Both the Guava library and Scala language have a lot else to recommend them, but learning a whole new library/language might be overboard.

淡忘如思 2024-10-22 12:55:44

您可以使用初始化块:

class Foo {
   //using static initializer block 
   static Map<String,String> m1 = new HashMap<String,String>();
   static {
      m1.put("x","y");
      m1.put("a","b");
   }    

   //using initializer block 
   Map<String,String> m2 = new HashMap<String,String>();
   {
      m2.put("x","y");
      m2.put("a","b");
   }    

} 

You can use initializer blocks:

class Foo {
   //using static initializer block 
   static Map<String,String> m1 = new HashMap<String,String>();
   static {
      m1.put("x","y");
      m1.put("a","b");
   }    

   //using initializer block 
   Map<String,String> m2 = new HashMap<String,String>();
   {
      m2.put("x","y");
      m2.put("a","b");
   }    

} 
鹊巢 2024-10-22 12:55:44

一些非常hacky的东西..可以改进,但这只是一个方向:
定义一个静态助手来将对象数组转换为这种类型的映射:

    public static<K,V> Map<K, V> fromArray(Object[] anObjArray){
    int size = anObjArray.length;
    Map<K, V> aMap = new HashMap<K, V>();
    for (int i=0;i<=size/2;i=i+2){
        K key = (K)anObjArray[i];
        V value = (V)anObjArray[i+1];
        aMap.put(key, value);
    }
    return aMap;
}

然后您可以使用它创建一个映射:

        Map<Integer, String> aMap = MapUtils.<Integer, String>fromArray(new Object[]{1, "one", 2,"two"});

我个人会赞同 @Carl 的 Gauva 构建器建议:-)

Something very hacky..can be improved, but this is just a direction:
Define a static helper to convert an object array to a map of this type:

    public static<K,V> Map<K, V> fromArray(Object[] anObjArray){
    int size = anObjArray.length;
    Map<K, V> aMap = new HashMap<K, V>();
    for (int i=0;i<=size/2;i=i+2){
        K key = (K)anObjArray[i];
        V value = (V)anObjArray[i+1];
        aMap.put(key, value);
    }
    return aMap;
}

then you can create a map using this:

        Map<Integer, String> aMap = MapUtils.<Integer, String>fromArray(new Object[]{1, "one", 2,"two"});

I would personally second Gauva builder suggestion from @Carl though :-)

萌能量女王 2024-10-22 12:55:43

您可以使用这种语法技巧:

Map<String,String> map = new HashMap<String,String>() {{
    put("x", "y");
    put("a", "b");
}};

不过,不太令人愉快。这将创建 HashMap 的匿名子类,并将其填充到实例初始值设定项中。

You can, sort of, using this syntax trick:

Map<String,String> map = new HashMap<String,String>() {{
    put("x", "y");
    put("a", "b");
}};

Not very pleasant, though. This creates an anonymous subclass of HashMap, and populates it in the instance initializer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文