如何直接初始化一个HashMap(以文字方式)? [英] How to directly initialize a HashMap (in a literal way)?

查看:159
本文介绍了如何直接初始化一个HashMap(以文字方式)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Map< String,String> test = 
new HashMap< String,String> {test:test,test:test};

什么是正确的语法?我没有发现任何事情。这可能吗?我正在寻找最短/精确的方法,在地图中放置一些最终/静态值,这些地图永远不会改变,并且在创建地图时提前知道。

解决方案

不,您必须手动添加所有元素。您可以使用静态初始化程序:

  public class Demo 
{
private static final Map< String ,String> MYMAP;
static
{
myMap = new HashMap< String,String>();
myMap.put(a,b);
myMap.put(c,d);
}
}

请注意,使用初始化函数会做同样的事情但可以提高代码的可读性:

  public class Demo 
{
private static final Map< String ,String> myMap = createMap();
private static Map< String,String> createMap()
{
Map< String,String> myMap = new HashMap< String,String>();
myMap.put(a,b);
myMap.put(c,d);
return myMap;
}
}



Java 9



在Java 9中,添加了几种工厂方法,也可以用于简化地图的创建:

  public class Demo {
private static final Map< String,String> test = Map.of(a,b,c,d);
private static final Map< String,String> test2 = Map.ofEntries(
entry(a,b),
条目(c,d)
);
}

在上面的例子中, test test2 将是一样的,只是用不同的方法来表达地图。 Map.of 方法定义在地图中最多十个元素,而 Map.ofEntries 方法将具有没有这样的限制。



请注意,在这种情况下,生成的地图将是不可变的地图。如果您希望地图是可变的,您可以再次复制地图。使用 mutableMap = new HashMap(Map.of(a,b));



(另见 JEP 269 Javadoc


Is there some way of initializing a Java HashMap like this?:

Map<String,String> test = 
    new HashMap<String, String>{"test":"test","test":"test"};

What would be the correct syntax? I have not found anything regarding this. Is this possible? I am looking for the shortest/fastet way to put some "final/static" values in a map that never change and are known in advance when crerating the Map.

解决方案

No, you will have to add all the elements manually. You can use a static initializer though:

public class Demo
{
    private static final Map<String, String> myMap;
    static
    {
        myMap = new HashMap<String, String>();
        myMap.put("a", "b");
        myMap.put("c", "d");
    }
}

Note that using a function for initialization will do the same but may improve readability of the code:

public class Demo
{
    private static final Map<String, String> myMap = createMap();
    private static Map<String, String> createMap()
    {
        Map<String,String> myMap = new HashMap<String,String>();
        myMap.put("a", "b");
        myMap.put("c", "d");
        return myMap;
    }
}

Java 9

In Java 9 a couple of factory-methods are added that can also be used to simplify the creation of maps:

public class Demo {
    private static final Map<String, String> test = Map.of("a", "b", "c", "d");
    private static final Map<String, String> test2 = Map.ofEntries(
        entry("a", "b"),
        entry("c", "d")
    );
}

In the example above both test and test2 will be the same, just with different ways of expressing the Map. The Map.of method is defined for up to ten elements in the map, while the Map.ofEntries method will have no such limit.

Note that in this case the resulting map will be an immutable map. If you want the map to be mutable, you could copy it again, e.g. using mutableMap = new HashMap<>(Map.of("a", "b"));

(See also JEP 269 and the Javadoc)

这篇关于如何直接初始化一个HashMap(以文字方式)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆