如何初始化静态地图?

发布于 2024-07-13 07:05:50 字数 571 浏览 7 评论 0 原文

如何在 Java 中初始化静态 Map

方法一:静态初始化
方法二:实例初始化(匿名子类) 或者 还有其他方法吗?

各自的优点和缺点是什么?

这是说明这两种方法的示例:

import java.util.HashMap;
import java.util.Map;

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

    private static final Map<Integer, String> myMap2 = new HashMap<>(){
        {
            put(1, "one");
            put(2, "two");
        }
    };
}

How would you initialise a static Map in Java?

Method one: static initialiser
Method two: instance initialiser (anonymous subclass)
or
some other method?

What are the pros and cons of each?

Here is an example illustrating the two methods:

import java.util.HashMap;
import java.util.Map;

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

    private static final Map<Integer, String> myMap2 = new HashMap<>(){
        {
            put(1, "one");
            put(2, "two");
        }
    };
}

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

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

发布评论

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

评论(30

愛上了 2024-07-20 07:05:50

在这种情况下,实例初始化程序只是语法糖,对吧? 我不明白为什么你需要一个额外的匿名类来初始化。 如果正在创建的类是最终类,则它将不起作用。

您也可以使用静态初始化程序创建不可变映射:

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);
    }
}

The instance initialiser is just syntactic sugar in this case, right? I don't see why you need an extra anonymous class just to initialize. And it won't work if the class being created is final.

You can create an immutable map using a static initialiser too:

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);
    }
}
×纯※雪 2024-07-20 07:05:50

我喜欢 Guava 初始化静态、不可变映射的方式:

static final Map<Integer, String> MY_MAP = ImmutableMap.of(
    1, "one",
    2, "two"
);

如您所见,它非常简洁(因为< 中方便的工厂方法代码>ImmutableMap)。

如果您希望映射有超过 5 个条目,则不能再使用 ImmutableMap.of()。 相反,请尝试 ImmutableMap.builder() 大致思路如下:

static final Map<Integer, String> MY_MAP = ImmutableMap.<Integer, String>builder()
    .put(1, "one")
    .put(2, "two")
    // ... 
    .put(15, "fifteen")
    .build();

要了解有关 Guava 不可变集合实用程序的优势的更多信息,请参阅 Guava 用户指南中的不可变集合解释

Guava(Guava 的一个子集)曾经被称为Google Collections。 如果您尚未在 Java 项目中使用此库,我强烈建议尝试一下! Guava 已迅速成为 Java 中最受欢迎和最有用的免费第 3 方库之一,如 SO用户同意。 (如果您是新手,该链接后面有一些优秀的学习资源。)


更新(2015):至于Java 8,嗯,我仍然会使用番石榴之所以采用,是因为它比其他任何东西都干净得多。 如果您不希望 Guava 依赖,请考虑使用普通的旧初始化方法。 如果你问我的话,二维数组和 Stream API 的破解是相当丑陋的,如果你需要创建的话,会变得更丑陋其键和值不是同一类型的 Map(如问题中的 Map)。

至于 Guava 的未来,关于 Java 8,Louis Wasserman 早在 2014 年就说过,并在 2016 年[更新]宣布Guava 21 将需要并正确支持 Java 8


更新(2016):如塔吉尔Valeev 指出Java 9 最终将通过添加 集合的便捷工厂方法

static final Map<Integer, String> MY_MAP = Map.of(
    1, "one", 
    2, "two"
);

I like the Guava way of initialising a static, immutable map:

static final Map<Integer, String> MY_MAP = ImmutableMap.of(
    1, "one",
    2, "two"
);

As you can see, it's very concise (because of the convenient factory methods in ImmutableMap).

If you want the map to have more than 5 entries, you can no longer use ImmutableMap.of(). Instead, try ImmutableMap.builder() along these lines:

static final Map<Integer, String> MY_MAP = ImmutableMap.<Integer, String>builder()
    .put(1, "one")
    .put(2, "two")
    // ... 
    .put(15, "fifteen")
    .build();

To learn more about the benefits of Guava's immutable collection utilities, see Immutable Collections Explained in Guava User Guide.

(A subset of) Guava used to be called Google Collections. If you aren't using this library in your Java project yet, I strongly recommend trying it out! Guava has quickly become one of the most popular and useful free 3rd party libs for Java, as fellow SO users agree. (If you are new to it, there are some excellent learning resources behind that link.)


Update (2015): As for Java 8, well, I would still use the Guava approach because it is way cleaner than anything else. If you don't want Guava dependency, consider a plain old init method. The hack with two-dimensional array and Stream API is pretty ugly if you ask me, and gets uglier if you need to create a Map whose keys and values are not the same type (like Map<Integer, String> in the question).

As for future of Guava in general, with regards to Java 8, Louis Wasserman said this back in 2014, and [update] in 2016 it was announced that Guava 21 will require and properly support Java 8.


Update (2016): As Tagir Valeev points out, Java 9 will finally make this clean to do using nothing but pure JDK, by adding convenience factory methods for collections:

static final Map<Integer, String> MY_MAP = Map.of(
    1, "one", 
    2, "two"
);
メ斷腸人バ 2024-07-20 07:05:50

我会使用:

public class Test {
    private static final Map<Integer, String> MY_MAP = createMap();

    private static Map<Integer, String> createMap() {
        Map<Integer, String> result = new HashMap<>();
        result.put(1, "one");
        result.put(2, "two");
        return Collections.unmodifiableMap(result);
    }
}
  1. 它避免使用匿名类,我个人认为这是一种不好的风格,并且避免
  2. 它使地图的创建更加明确
  3. 它使地图不可修改,
  4. 因为 MY_MAP 是常量,我将其命名为常量

I would use:

public class Test {
    private static final Map<Integer, String> MY_MAP = createMap();

    private static Map<Integer, String> createMap() {
        Map<Integer, String> result = new HashMap<>();
        result.put(1, "one");
        result.put(2, "two");
        return Collections.unmodifiableMap(result);
    }
}
  1. it avoids an anonymous class, which I personally consider to be a bad style, and avoid
  2. it makes the creation of map more explicit
  3. it makes map unmodifiable
  4. as MY_MAP is constant, I would name it like constant
夕嗳→ 2024-07-20 07:05:50

Java 5 提供了这种更紧凑的语法:

static final Map<String , String> FLAVORS = new HashMap<String , String>() {{
    put("Up",    "Down");
    put("Charm", "Strange");
    put("Top",   "Bottom");
}};

Java 5 provides this more compact syntax:

static final Map<String , String> FLAVORS = new HashMap<String , String>() {{
    put("Up",    "Down");
    put("Charm", "Strange");
    put("Top",   "Bottom");
}};
捎一片雪花 2024-07-20 07:05:50

第二种方法的一个优点是,您可以用 Collections.unmodifyingMap() 包装它,以保证以后不会有任何内容更新集合:

private static final Map<Integer, String> CONSTANT_MAP = 
    Collections.unmodifiableMap(new HashMap<Integer, String>() {{ 
        put(1, "one");
        put(2, "two");
    }});

 // later on...

 CONSTANT_MAP.put(3, "three"); // going to throw an exception!

One advantage to the second method is that you can wrap it with Collections.unmodifiableMap() to guarantee that nothing is going to update the collection later:

private static final Map<Integer, String> CONSTANT_MAP = 
    Collections.unmodifiableMap(new HashMap<Integer, String>() {{ 
        put(1, "one");
        put(2, "two");
    }});

 // later on...

 CONSTANT_MAP.put(3, "three"); // going to throw an exception!
诗化ㄋ丶相逢 2024-07-20 07:05:50

Java 9+ 中的 Map.of

private static final Map<Integer, String> MY_MAP = Map.of(1, "one", 2, "two");

有关详细信息,请参阅 JEP 269。 JDK 9 于 2017 年 9 月正式发布

Map.of in Java 9+

private static final Map<Integer, String> MY_MAP = Map.of(1, "one", 2, "two");

See JEP 269 for details. JDK 9 reached general availability in September 2017.

微暖i 2024-07-20 07:05:50

这是一个 Java 8 单行静态映射初始值设定项:

private static final Map<String, String> EXTENSION_TO_MIMETYPE =
    Arrays.stream(new String[][] {
        { "txt", "text/plain" }, 
        { "html", "text/html" }, 
        { "js", "application/javascript" },
        { "css", "text/css" },
        { "xml", "application/xml" },
        { "png", "image/png" }, 
        { "gif", "image/gif" }, 
        { "jpg", "image/jpeg" },
        { "jpeg", "image/jpeg" }, 
        { "svg", "image/svg+xml" },
    }).collect(Collectors.toMap(kv -> kv[0], kv -> kv[1]));

编辑:要像问题中那样初始化 Map,您需要这样的内容:

static final Map<Integer, String> MY_MAP = Arrays.stream(new Object[][]{
        {1, "one"},
        {2, "two"},
}).collect(Collectors.toMap(kv -> (Integer) kv[0], kv -> (String) kv[1]));

编辑(2):有一个i_am_zero 提供的更好的混合类型版本,它使用 new SimpleEntry<>(k, v) 调用流。 查看该答案:https://stackoverflow.com/a/37384773/3950982

Here's a Java 8 one-line static map initializer:

private static final Map<String, String> EXTENSION_TO_MIMETYPE =
    Arrays.stream(new String[][] {
        { "txt", "text/plain" }, 
        { "html", "text/html" }, 
        { "js", "application/javascript" },
        { "css", "text/css" },
        { "xml", "application/xml" },
        { "png", "image/png" }, 
        { "gif", "image/gif" }, 
        { "jpg", "image/jpeg" },
        { "jpeg", "image/jpeg" }, 
        { "svg", "image/svg+xml" },
    }).collect(Collectors.toMap(kv -> kv[0], kv -> kv[1]));

Edit: to initialize a Map<Integer, String> as in the question, you'd need something like this:

static final Map<Integer, String> MY_MAP = Arrays.stream(new Object[][]{
        {1, "one"},
        {2, "two"},
}).collect(Collectors.toMap(kv -> (Integer) kv[0], kv -> (String) kv[1]));

Edit(2): There is a better, mixed-type-capable version by i_am_zero that uses a stream of new SimpleEntry<>(k, v) calls. Check out that answer: https://stackoverflow.com/a/37384773/3950982

旧伤还要旧人安 2024-07-20 07:05:50

Java 9

我们可以使用 Map.ofEntries,调用 Map.entry( k , v ) 创建每个条目。

import static java.util.Map.entry;
private static final Map<Integer,String> map = Map.ofEntries(
        entry(1, "one"),
        entry(2, "two"),
        entry(3, "three"),
        entry(4, "four"),
        entry(5, "five"),
        entry(6, "six"),
        entry(7, "seven"),
        entry(8, "eight"),
        entry(9, "nine"),
        entry(10, "ten"));

我们还可以按照 Tagir 在他的回答这里中的建议使用Map.of,但我们不能使用 Map.of 的条目超过 10 个。

Java 8

我们可以创建映射条目流。 我们已经在 java.util.AbstractMap 中有两个 Entry 实现,它们是 SimpleEntrySimpleImmutableEntry。 对于这个例子,我们可以使用前者作为:

import java.util.AbstractMap.*;
private static final Map<Integer, String> myMap = Stream.of(
            new SimpleEntry<>(1, "one"),
            new SimpleEntry<>(2, "two"),
            new SimpleEntry<>(3, "three"),
            new SimpleEntry<>(4, "four"),
            new SimpleEntry<>(5, "five"),
            new SimpleEntry<>(6, "six"),
            new SimpleEntry<>(7, "seven"),
            new SimpleEntry<>(8, "eight"),
            new SimpleEntry<>(9, "nine"),
            new SimpleEntry<>(10, "ten"))
            .collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue));
            

Java 9

We can use Map.ofEntries, calling Map.entry( k , v ) to create each entry.

import static java.util.Map.entry;
private static final Map<Integer,String> map = Map.ofEntries(
        entry(1, "one"),
        entry(2, "two"),
        entry(3, "three"),
        entry(4, "four"),
        entry(5, "five"),
        entry(6, "six"),
        entry(7, "seven"),
        entry(8, "eight"),
        entry(9, "nine"),
        entry(10, "ten"));

We can also use Map.of as suggested by Tagir in his answer here but we cannot have more than 10 entries using Map.of.

Java 8

We can create a Stream of map entries. We already have two implementations of Entry in java.util.AbstractMap which are SimpleEntry and SimpleImmutableEntry. For this example we can make use of former as:

import java.util.AbstractMap.*;
private static final Map<Integer, String> myMap = Stream.of(
            new SimpleEntry<>(1, "one"),
            new SimpleEntry<>(2, "two"),
            new SimpleEntry<>(3, "three"),
            new SimpleEntry<>(4, "four"),
            new SimpleEntry<>(5, "five"),
            new SimpleEntry<>(6, "six"),
            new SimpleEntry<>(7, "seven"),
            new SimpleEntry<>(8, "eight"),
            new SimpleEntry<>(9, "nine"),
            new SimpleEntry<>(10, "ten"))
            .collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue));
            
怀中猫帐中妖 2024-07-20 07:05:50

使用 Eclipse Collections,以下所有功能都将起作用:

import java.util.Map;

import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.impl.factory.Maps;

public class StaticMapsTest
{
    private static final Map<Integer, String> MAP =
        Maps.mutable.with(1, "one", 2, "two");

    private static final MutableMap<Integer, String> MUTABLE_MAP =
       Maps.mutable.with(1, "one", 2, "two");


    private static final MutableMap<Integer, String> UNMODIFIABLE_MAP =
        Maps.mutable.with(1, "one", 2, "two").asUnmodifiable();


    private static final MutableMap<Integer, String> SYNCHRONIZED_MAP =
        Maps.mutable.with(1, "one", 2, "two").asSynchronized();


    private static final ImmutableMap<Integer, String> IMMUTABLE_MAP =
        Maps.mutable.with(1, "one", 2, "two").toImmutable();


    private static final ImmutableMap<Integer, String> IMMUTABLE_MAP2 =
        Maps.immutable.with(1, "one", 2, "two");
}

您还可以使用 Eclipse 静态初始化原始映射收藏。

import org.eclipse.collections.api.map.primitive.ImmutableIntObjectMap;
import org.eclipse.collections.api.map.primitive.MutableIntObjectMap;
import org.eclipse.collections.impl.factory.primitive.IntObjectMaps;

public class StaticPrimitiveMapsTest
{
    private static final MutableIntObjectMap<String> MUTABLE_INT_OBJ_MAP =
            IntObjectMaps.mutable.<String>empty()
                    .withKeyValue(1, "one")
                    .withKeyValue(2, "two");

    private static final MutableIntObjectMap<String> UNMODIFIABLE_INT_OBJ_MAP =
            IntObjectMaps.mutable.<String>empty()
                    .withKeyValue(1, "one")
                    .withKeyValue(2, "two")
                    .asUnmodifiable();

    private static final MutableIntObjectMap<String> SYNCHRONIZED_INT_OBJ_MAP =
            IntObjectMaps.mutable.<String>empty()
                    .withKeyValue(1, "one")
                    .withKeyValue(2, "two")
                    .asSynchronized();

    private static final ImmutableIntObjectMap<String> IMMUTABLE_INT_OBJ_MAP =
            IntObjectMaps.mutable.<String>empty()
                    .withKeyValue(1, "one")
                    .withKeyValue(2, "two")
                    .toImmutable();

    private static final ImmutableIntObjectMap<String> IMMUTABLE_INT_OBJ_MAP2 =
            IntObjectMaps.immutable.<String>empty()
                    .newWithKeyValue(1, "one")
                    .newWithKeyValue(2, "two");
} 

注意:我是 Eclipse Collections 的提交者

With Eclipse Collections, all of the following will work:

import java.util.Map;

import org.eclipse.collections.api.map.ImmutableMap;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.impl.factory.Maps;

public class StaticMapsTest
{
    private static final Map<Integer, String> MAP =
        Maps.mutable.with(1, "one", 2, "two");

    private static final MutableMap<Integer, String> MUTABLE_MAP =
       Maps.mutable.with(1, "one", 2, "two");


    private static final MutableMap<Integer, String> UNMODIFIABLE_MAP =
        Maps.mutable.with(1, "one", 2, "two").asUnmodifiable();


    private static final MutableMap<Integer, String> SYNCHRONIZED_MAP =
        Maps.mutable.with(1, "one", 2, "two").asSynchronized();


    private static final ImmutableMap<Integer, String> IMMUTABLE_MAP =
        Maps.mutable.with(1, "one", 2, "two").toImmutable();


    private static final ImmutableMap<Integer, String> IMMUTABLE_MAP2 =
        Maps.immutable.with(1, "one", 2, "two");
}

You can also statically initialize primitive maps with Eclipse Collections.

import org.eclipse.collections.api.map.primitive.ImmutableIntObjectMap;
import org.eclipse.collections.api.map.primitive.MutableIntObjectMap;
import org.eclipse.collections.impl.factory.primitive.IntObjectMaps;

public class StaticPrimitiveMapsTest
{
    private static final MutableIntObjectMap<String> MUTABLE_INT_OBJ_MAP =
            IntObjectMaps.mutable.<String>empty()
                    .withKeyValue(1, "one")
                    .withKeyValue(2, "two");

    private static final MutableIntObjectMap<String> UNMODIFIABLE_INT_OBJ_MAP =
            IntObjectMaps.mutable.<String>empty()
                    .withKeyValue(1, "one")
                    .withKeyValue(2, "two")
                    .asUnmodifiable();

    private static final MutableIntObjectMap<String> SYNCHRONIZED_INT_OBJ_MAP =
            IntObjectMaps.mutable.<String>empty()
                    .withKeyValue(1, "one")
                    .withKeyValue(2, "two")
                    .asSynchronized();

    private static final ImmutableIntObjectMap<String> IMMUTABLE_INT_OBJ_MAP =
            IntObjectMaps.mutable.<String>empty()
                    .withKeyValue(1, "one")
                    .withKeyValue(2, "two")
                    .toImmutable();

    private static final ImmutableIntObjectMap<String> IMMUTABLE_INT_OBJ_MAP2 =
            IntObjectMaps.immutable.<String>empty()
                    .newWithKeyValue(1, "one")
                    .newWithKeyValue(2, "two");
} 

Note: I am a committer for Eclipse Collections

蝶舞 2024-07-20 07:05:50

在这种情况下我永远不会创建匿名子类。 如果您想让映射不可修改,静态初始值设定项同样可以很好地工作,例如:

private static final Map<Integer, String> MY_MAP;
static
{
    Map<Integer, String>tempMap = new HashMap<Integer, String>();
    tempMap.put(1, "one");
    tempMap.put(2, "two");
    MY_MAP = Collections.unmodifiableMap(tempMap);
}

I would never create an anonymous subclass in this situation. Static initializers work equally well, if you would like to make the map unmodifiable for example:

private static final Map<Integer, String> MY_MAP;
static
{
    Map<Integer, String>tempMap = new HashMap<Integer, String>();
    tempMap.put(1, "one");
    tempMap.put(2, "two");
    MY_MAP = Collections.unmodifiableMap(tempMap);
}
孤寂小茶 2024-07-20 07:05:50

我喜欢匿名类,因为它很容易处理:

public static final Map<?, ?> numbers = Collections.unmodifiableMap(new HashMap<Integer, String>() {
    {
        put(1, "some value");
                    //rest of code here
    }
});

I like anonymous class, because it is easy to deal with it:

public static final Map<?, ?> numbers = Collections.unmodifiableMap(new HashMap<Integer, String>() {
    {
        put(1, "some value");
                    //rest of code here
    }
});
一张白纸 2024-07-20 07:05:50

也许查看 Google 收藏会很有趣,例如他们页面上的视频。 它们提供了各种初始化映射和集合的方法,并提供不可变的集合。

更新:该库现已命名为 Guava

Maybe it's interesting to check out Google Collections, e.g. the videos that they have on their page. They provide various ways to initialize maps and sets, and provide immutable collections as well.

Update: This library is now named Guava.

沫雨熙 2024-07-20 07:05:50
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);
    }
}

如果我们声明多个常量,那么该代码将被编写在静态块中,这在将来很难维护。 所以最好使用匿名类。

public class Test {

    public static final Map numbers = Collections.unmodifiableMap(new HashMap(2, 1.0f){
        {
            put(1, "one");
            put(2, "two");
        }
    });
}

建议对常量使用 unmodificableMap,否则不能将其视为常量。

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);
    }
}

If we declare more than one constant then that code will be written in static block and that is hard to maintain in future. So it is better to use anonymous class.

public class Test {

    public static final Map numbers = Collections.unmodifiableMap(new HashMap(2, 1.0f){
        {
            put(1, "one");
            put(2, "two");
        }
    });
}

And it is suggested to used unmodifiableMap for constants other wise it can't be treated as constant.

如若梦似彩虹 2024-07-20 07:05:50

我强烈建议使用“双括号初始化”样式而不是静态块样式。

也许有人会说不喜欢匿名类、不喜欢开销、不喜欢性能等,

但我更考虑的是代码的可读性和可维护性。 从这个角度来看,我认为双括号是比静态方法更好的代码风格。

  1. 元素是嵌套和内联的。
  2. 它更加面向对象,而不是过程化。
  3. 对性能的影响非常小,可以忽略不计。
  4. 更好的 IDE 大纲支持(而不是许多匿名 static{} 块)
  5. 您保存了几行注释来将它们联系起来。
  6. 防止异常和字节码优化器可能导致元素泄漏/实例导致未初始化对象。
  7. 不用担心静态块的执行顺序。

另外,如果您知道匿名类的GC,您始终可以使用new HashMap(Map map)将其转换为普通的HashMap。

您可以这样做,直到遇到另一个问题为止。 如果这样做,您应该使用另一种编码风格(例如,无静态、工厂类)。

I could strongly suggest the "double brace initialization" style over static block style.

Someone may comment that they don't like anonymous class, overhead, performance, etc.

But that I more consider is the code readability and maintainability. In this point of view, I stand a double brace is a better code style rather then static method.

  1. The elements are nested and inline.
  2. It is more OO, not procedural.
  3. the performance impact is really small and could be ignored.
  4. Better IDE outline support (rather then many anonymous static{} block)
  5. You saved few lines of comment to bring them relationship.
  6. Prevent possible element leak/instance lead of uninitialized object from exception and bytecode optimizer.
  7. No worry about the order of execution of static block.

In addition, it you aware the GC of the anonymous class, you can always convert it to a normal HashMap by using new HashMap(Map map).

You can do this until you faced another problem. If you do, you should use complete another coding style (e.g. no static, factory class) for it.

昇り龍 2024-07-20 07:05:50

像往常一样 apache-commons 有正确的方法 MapUtils.putAll(Map, Object[])

例如,创建彩色图:

Map<String, String> colorMap = MapUtils.putAll(new HashMap<String, String>(), new String[][] {
     {"RED", "#FF0000"},
     {"GREEN", "#00FF00"},
     {"BLUE", "#0000FF"}
 });

As usual apache-commons has proper method MapUtils.putAll(Map, Object[]):

For example, to create a color map:

Map<String, String> colorMap = MapUtils.putAll(new HashMap<String, String>(), new String[][] {
     {"RED", "#FF0000"},
     {"GREEN", "#00FF00"},
     {"BLUE", "#0000FF"}
 });
水晶透心 2024-07-20 07:05:50

如果我

  • 不想(或不能)使用 Guava 的 ImmutableMap.of()
  • 或者我需要一个可变的 Map
  • 或者我需要超过 10 个条目限制, 那么这是我最喜欢的JDK9+ 中的 Map.of()
public static <A> Map<String, A> asMap(Object... keysAndValues) {
  return new LinkedHashMap<String, A>() {{
    for (int i = 0; i < keysAndValues.length - 1; i++) {
      put(keysAndValues[i].toString(), (A) keysAndValues[++i]);
    }
  }};
}

非常紧凑,并且它忽略杂散值(即没有值的最终键)。

用法:

Map<String, String> one = asMap("1stKey", "1stVal", "2ndKey", "2ndVal");
Map<String, Object> two = asMap("1stKey", Boolean.TRUE, "2ndKey", new Integer(2));

Here's my favorite if I

  • don't want to (or cannot) use Guava's ImmutableMap.of()
  • or I need a mutable Map
  • or I need more than the 10 entry limit in Map.of() from JDK9+
public static <A> Map<String, A> asMap(Object... keysAndValues) {
  return new LinkedHashMap<String, A>() {{
    for (int i = 0; i < keysAndValues.length - 1; i++) {
      put(keysAndValues[i].toString(), (A) keysAndValues[++i]);
    }
  }};
}

It's very compact, and it ignores stray values (i.e. a final key without a value).

Usage:

Map<String, String> one = asMap("1stKey", "1stVal", "2ndKey", "2ndVal");
Map<String, Object> two = asMap("1stKey", Boolean.TRUE, "2ndKey", new Integer(2));
ζ澈沫 2024-07-20 07:05:50

我更喜欢使用静态初始值设定项来避免生成匿名类(这将没有进一步的目的),因此我将列出使用静态初始值设定项进行初始化的提示。 所有列出的解决方案/提示都是类型安全的。

注意:这个问题没有提到任何关于使地图不可修改的内容,所以我将省略它,但知道它可以使用 Collections.unmodifyingMap(map)

第一个技巧

第一个技巧是您可以对地图进行本地引用并为其指定一个简短名称:

private static final Map<Integer, String> myMap = new HashMap<>();
static {
    final Map<Integer, String> m = myMap; // Use short name!
    m.put(1, "one"); // Here referencing the local variable which is also faster!
    m.put(2, "two");
    m.put(3, "three");
}

第二个技巧

第二个技巧是您可以创建一个助手添加条目的方法; 如果您愿意,也可以将此辅助方法公开:

private static final Map<Integer, String> myMap2 = new HashMap<>();
static {
    p(1, "one"); // Calling the helper method.
    p(2, "two");
    p(3, "three");
}

private static void p(Integer k, String v) {
    myMap2.put(k, v);
}

不过,此处的辅助方法不可重复使用,因为它只能向 myMap2 添加元素。 为了使其可重用,我们可以将映射本身作为辅助方法的参数,但初始化代码不会更短。

第三个技巧

第三个技巧是您可以创建一个具有填充功能的可重用的类似构建器的帮助器类。 这实际上是一个简单的 10 行帮助器类,它是类型安全的:

public class Test {
    private static final Map<Integer, String> myMap3 = new HashMap<>();
    static {
        new B<>(myMap3)   // Instantiating the helper class with our map
            .p(1, "one")
            .p(2, "two")
            .p(3, "three");
    }
}

class B<K, V> {
    private final Map<K, V> m;

    public B(Map<K, V> m) {
        this.m = m;
    }

    public B<K, V> p(K k, V v) {
        m.put(k, v);
        return this; // Return this for chaining
    }
}

I prefer using a static initializer to avoid generating anonymous classes (which would have no further purpose), so I'll list tips initializing with a static initializer. All listed solutions / tips are type-safe.

Note: The question doesn't say anything about making the map unmodifiable, so I will leave that out, but know that it can easily be done with Collections.unmodifiableMap(map).

First tip

The 1st tip is that you can make a local reference to the map and you give it a SHORT name:

private static final Map<Integer, String> myMap = new HashMap<>();
static {
    final Map<Integer, String> m = myMap; // Use short name!
    m.put(1, "one"); // Here referencing the local variable which is also faster!
    m.put(2, "two");
    m.put(3, "three");
}

Second tip

The 2nd tip is that you can create a helper method to add entries; you can also make this helper method public if you want to:

private static final Map<Integer, String> myMap2 = new HashMap<>();
static {
    p(1, "one"); // Calling the helper method.
    p(2, "two");
    p(3, "three");
}

private static void p(Integer k, String v) {
    myMap2.put(k, v);
}

The helper method here is not re-usable though because it can only add elements to myMap2. To make it re-usable, we could make the map itself a parameter of the helper method, but then initialization code would not be any shorter.

Third tip

The 3rd tip is that you can create a re-usable builder-like helper class with the populating functionality. This is really a simple, 10-line helper class which is type-safe:

public class Test {
    private static final Map<Integer, String> myMap3 = new HashMap<>();
    static {
        new B<>(myMap3)   // Instantiating the helper class with our map
            .p(1, "one")
            .p(2, "two")
            .p(3, "three");
    }
}

class B<K, V> {
    private final Map<K, V> m;

    public B(Map<K, V> m) {
        this.m = m;
    }

    public B<K, V> p(K k, V v) {
        m.put(k, v);
        return this; // Return this for chaining
    }
}
甜心 2024-07-20 07:05:50

如果你想要不可修改的地图,最后java 9在Map接口中添加了一个很酷的工厂方法of。 Set、List也添加了类似的方法。

地图<字符串,字符串> unmodifierMap = Map.of("key1", "value1", "key2", "value2");

If you want unmodifiable map, finally java 9 added a cool factory method of to Map interface. Similar method is added to Set, List as well.

Map<String, String> unmodifiableMap = Map.of("key1", "value1", "key2", "value2");

就此别过 2024-07-20 07:05:50

您创建的匿名类效果很好。 但是您应该知道这是一个内部类,因此它将包含对周围类实例的引用。 所以你会发现你不能用它做某些事情(使用 XStream 就是其中之一)。 你会得到一些非常奇怪的错误。

话虽如此,只要您意识到,这种方法就可以了。 我大部分时间都用它来以简洁的方式初始化各种集合。

编辑:在注释中正确指出这是一个静态类。 显然我没有仔细阅读这篇文章。 然而,我的评论do仍然适用于匿名内部类。

The anonymous class you're creating works well. However you should be aware that this is an inner class and as such, it'll contain a reference to the surrounding class instance. So you'll find you can't do certain things with it (using XStream for one). You'll get some very strange errors.

Having said that, so long as you're aware then this approach is fine. I use it most of the time for initialising all sorts of collections in a concise fashion.

EDIT: Pointed out correctly in the comments that this is a static class. Obviously I didn't read this closely enough. However my comments do still apply to anonymous inner classes.

君勿笑 2024-07-20 07:05:50

如果您想要简洁且相对安全的东西,您可以将编译时类型检查转移到运行时:

static final Map<String, Integer> map = MapUtils.unmodifiableMap(
    String.class, Integer.class,
    "cat",  4,
    "dog",  2,
    "frog", 17
);

此实现应该捕获任何错误:

import java.util.HashMap;

public abstract class MapUtils
{
    private MapUtils() { }

    public static <K, V> HashMap<K, V> unmodifiableMap(
            Class<? extends K> keyClazz,
            Class<? extends V> valClazz,
            Object...keyValues)
    {
        return Collections.<K, V>unmodifiableMap(makeMap(
            keyClazz,
            valClazz,
            keyValues));
    }

    public static <K, V> HashMap<K, V> makeMap(
            Class<? extends K> keyClazz,
            Class<? extends V> valClazz,
            Object...keyValues)
    {
        if (keyValues.length % 2 != 0)
        {
            throw new IllegalArgumentException(
                    "'keyValues' was formatted incorrectly!  "
                  + "(Expected an even length, but found '" + keyValues.length + "')");
        }

        HashMap<K, V> result = new HashMap<K, V>(keyValues.length / 2);

        for (int i = 0; i < keyValues.length;)
        {
            K key = cast(keyClazz, keyValues[i], i);
            ++i;
            V val = cast(valClazz, keyValues[i], i);
            ++i;
            result.put(key, val);
        }

        return result;
    }

    private static <T> T cast(Class<? extends T> clazz, Object object, int i)
    {
        try
        {
            return clazz.cast(object);
        }
        catch (ClassCastException e)
        {
            String objectName = (i % 2 == 0) ? "Key" : "Value";
            String format = "%s at index %d ('%s') wasn't assignable to type '%s'";
            throw new IllegalArgumentException(String.format(format, objectName, i, object.toString(), clazz.getSimpleName()), e);
        }
    }
}

If you want something terse and relatively safe, you can just shift compile-time type checking to run-time:

static final Map<String, Integer> map = MapUtils.unmodifiableMap(
    String.class, Integer.class,
    "cat",  4,
    "dog",  2,
    "frog", 17
);

This implementation should catch any errors:

import java.util.HashMap;

public abstract class MapUtils
{
    private MapUtils() { }

    public static <K, V> HashMap<K, V> unmodifiableMap(
            Class<? extends K> keyClazz,
            Class<? extends V> valClazz,
            Object...keyValues)
    {
        return Collections.<K, V>unmodifiableMap(makeMap(
            keyClazz,
            valClazz,
            keyValues));
    }

    public static <K, V> HashMap<K, V> makeMap(
            Class<? extends K> keyClazz,
            Class<? extends V> valClazz,
            Object...keyValues)
    {
        if (keyValues.length % 2 != 0)
        {
            throw new IllegalArgumentException(
                    "'keyValues' was formatted incorrectly!  "
                  + "(Expected an even length, but found '" + keyValues.length + "')");
        }

        HashMap<K, V> result = new HashMap<K, V>(keyValues.length / 2);

        for (int i = 0; i < keyValues.length;)
        {
            K key = cast(keyClazz, keyValues[i], i);
            ++i;
            V val = cast(valClazz, keyValues[i], i);
            ++i;
            result.put(key, val);
        }

        return result;
    }

    private static <T> T cast(Class<? extends T> clazz, Object object, int i)
    {
        try
        {
            return clazz.cast(object);
        }
        catch (ClassCastException e)
        {
            String objectName = (i % 2 == 0) ? "Key" : "Value";
            String format = "%s at index %d ('%s') wasn't assignable to type '%s'";
            throw new IllegalArgumentException(String.format(format, objectName, i, object.toString(), clazz.getSimpleName()), e);
        }
    }
}
无名指的心愿 2024-07-20 07:05:50

对于 Java 8,我开始使用以下模式:

private static final Map<String, Integer> MAP = Stream.of(
    new AbstractMap.SimpleImmutableEntry<>("key1", 1),
    new AbstractMap.SimpleImmutableEntry<>("key2", 2)
).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

它不是最简洁且有点迂回的模式,但

  • 它不需要 java.util 之外的任何内容,
  • 它是类型安全的,并且可以轻松容纳不同的类型键和值。

With Java 8 I've come to use the following pattern:

private static final Map<String, Integer> MAP = Stream.of(
    new AbstractMap.SimpleImmutableEntry<>("key1", 1),
    new AbstractMap.SimpleImmutableEntry<>("key2", 2)
).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

It's not the most terse and a bit roundabout, but

  • it doesn't require anything outside of java.util
  • it's typesafe and easily accommodates different types for key and value.
困倦 2024-07-20 07:05:50

如果您只需要向映射添加一个值,可以使用 Collections.singletonMap

Map<K, V> map = Collections.singletonMap(key, value)

If you only need to add one value to the map you can use Collections.singletonMap:

Map<K, V> map = Collections.singletonMap(key, value)
缺⑴份安定 2024-07-20 07:05:50

您可以使用 StickyMapMapEntry 来自 Cactoos

private static final Map<String, String> MAP = new StickyMap<>(
  new MapEntry<>("name", "Jeffrey"),
  new MapEntry<>("age", "35")
);

You may use StickyMap and MapEntry from Cactoos:

private static final Map<String, String> MAP = new StickyMap<>(
  new MapEntry<>("name", "Jeffrey"),
  new MapEntry<>("age", "35")
);
<逆流佳人身旁 2024-07-20 07:05:50

您的第二种方法(双括号初始化)被认为是反模式,所以我会采用第一种方法。

初始化静态 Map 的另一种简单方法是使用此实用程序函数:

public static <K, V> Map<K, V> mapOf(Object... keyValues) {
    Map<K, V> map = new HashMap<>(keyValues.length / 2);

    for (int index = 0; index < keyValues.length / 2; index++) {
        map.put((K)keyValues[index * 2], (V)keyValues[index * 2 + 1]);
    }

    return map;
}

Map<Integer, String> map1 = mapOf(1, "value1", 2, "value2");
Map<String, String> map2 = mapOf("key1", "value1", "key2", "value2");

注意:在 Java 9 中,您可以使用 Map.of

Your second approach (Double Brace initialization) is thought to be an anti pattern, so I would go for the first approach.

Another easy way to initialise a static Map is by using this utility function:

public static <K, V> Map<K, V> mapOf(Object... keyValues) {
    Map<K, V> map = new HashMap<>(keyValues.length / 2);

    for (int index = 0; index < keyValues.length / 2; index++) {
        map.put((K)keyValues[index * 2], (V)keyValues[index * 2 + 1]);
    }

    return map;
}

Map<Integer, String> map1 = mapOf(1, "value1", 2, "value2");
Map<String, String> map2 = mapOf("key1", "value1", "key2", "value2");

Note: in Java 9 you can use Map.of

别闹i 2024-07-20 07:05:50

我不喜欢静态初始化语法,也不相信匿名子类。 一般来说,我同意前面的答案中提到的使用静态初始值设定项的所有缺点以及使用匿名子类的所有缺点。 另一方面 - 这些帖子中介绍的优点对我来说还不够。 我更喜欢使用静态初始化方法:

public class MyClass {
    private static final Map<Integer, String> myMap = prepareMap();

    private static Map<Integer, String> prepareMap() {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "one");
        hashMap.put(2, "two");

        return hashMap;
    }
}

I do not like Static initializer syntax and I'm not convinced to anonymous subclasses. Generally, I agree with all cons of using Static initializers and all cons of using anonymous subclasses that were mentioned in previus answers. On the other hand - pros presented in these posts are not enough for me. I prefer to use static initialization method:

public class MyClass {
    private static final Map<Integer, String> myMap = prepareMap();

    private static Map<Integer, String> prepareMap() {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "one");
        hashMap.put(2, "two");

        return hashMap;
    }
}
书信已泛黄 2024-07-20 07:05:50

我还没有看到我在任何答案中使用的方法(并且已经变得喜欢),所以这里是:

我不喜欢使用静态初始化器,因为它们很笨重,
我不喜欢匿名类,因为它为每个实例创建一个新类。

相反,我更喜欢如下所示的初始化:

map(
    entry("keyA", "val1"),
    entry("keyB", "val2"),
    entry("keyC", "val3")
);

不幸的是,这些方法不是标准 Java 库的一部分,
因此您需要创建(或使用)一个定义以下方法的实用程序库:(

 public static <K,V> Map<K,V> map(Map.Entry<K, ? extends V>... entries)
 public static <K,V> Map.Entry<K,V> entry(K key, V val)

您可以使用“导入静态”来避免需要在方法名称前添加前缀)

我发现为其他集合提供类似的静态方法很有用( list、set、sortedSet、sortedMap 等)

它不如 json 对象初始化那么好,但就可读性而言,它是朝这个方向迈出的一步。

I have not seen the approach I use (and have grown to like) posted in any answers, so here it is:

I don't like using static initializers because they are clunky,
and I don't like anonymous classes because it is creating a new class for each instance.

instead, I prefer initialization that looks like this:

map(
    entry("keyA", "val1"),
    entry("keyB", "val2"),
    entry("keyC", "val3")
);

unfortunately, these methods are not part of the standard Java library,
so you will need to create (or use) a utility library that defines the following methods:

 public static <K,V> Map<K,V> map(Map.Entry<K, ? extends V>... entries)
 public static <K,V> Map.Entry<K,V> entry(K key, V val)

(you can use 'import static' to avoid needing to prefix the method's name)

I found it useful to provide similar static methods for the other collections (list, set, sortedSet, sortedMap, etc.)

Its not quite as nice as json object initialization, but it's a step in that direction, as far as readability is concerned.

恰似旧人归 2024-07-20 07:05:50

由于 Java 不支持映射文字,因此必须始终显式实例化和填充映射实例。

幸运的是,可以使用工厂方法来模拟 Java 中映射文字的行为。

例如:

public class LiteralMapFactory {

    // Creates a map from a list of entries
    @SafeVarargs
    public static <K, V> Map<K, V> mapOf(Map.Entry<K, V>... entries) {
        LinkedHashMap<K, V> map = new LinkedHashMap<>();
        for (Map.Entry<K, V> entry : entries) {
            map.put(entry.getKey(), entry.getValue());
        }
        return map;
    }
    // Creates a map entry
    public static <K, V> Map.Entry<K, V> entry(K key, V value) {
        return new AbstractMap.SimpleEntry<>(key, value);
    }

    public static void main(String[] args) {
        System.out.println(mapOf(entry("a", 1), entry("b", 2), entry("c", 3)));
    }
}

输出:

{a=1、b=2、c=3}

这比一次创建并填充地图一个元素要方便得多。

Because Java does not support map literals, map instances must always be explicitly instantiated and populated.

Fortunately, it is possible to approximate the behavior of map literals in Java using factory methods.

For example:

public class LiteralMapFactory {

    // Creates a map from a list of entries
    @SafeVarargs
    public static <K, V> Map<K, V> mapOf(Map.Entry<K, V>... entries) {
        LinkedHashMap<K, V> map = new LinkedHashMap<>();
        for (Map.Entry<K, V> entry : entries) {
            map.put(entry.getKey(), entry.getValue());
        }
        return map;
    }
    // Creates a map entry
    public static <K, V> Map.Entry<K, V> entry(K key, V value) {
        return new AbstractMap.SimpleEntry<>(key, value);
    }

    public static void main(String[] args) {
        System.out.println(mapOf(entry("a", 1), entry("b", 2), entry("c", 3)));
    }
}

Output:

{a=1, b=2, c=3}

It is a lot more convenient than creating and populating the map an element at a time.

软甜啾 2024-07-20 07:05:50

JEP 269 为 Collections API 提供了一些便捷的工厂方法。 该工厂方法不在当前的 Java 版本(即 8)中,但计划在 Java 9 版本中使用。

对于 Map 有两个工厂方法:ofofEntries。 使用of,您可以传递交替的键/值对。 例如,为了创建像{age: 27, Major: cs}这样的Map

Map<String, Object> info = Map.of("age", 27, "major", "cs");

目前of有十个重载版本,因此您可以创建一个包含十个键/值对的映射。 如果您不喜欢这种限制或交替键/值,可以使用 ofEntries

Map<String, Object> info = Map.ofEntries(
                Map.entry("age", 27),
                Map.entry("major", "cs")
);

ofofEntries 都将返回不可变的 Map,因此构建后您无法更改其元素。 您可以使用JDK 9 Early Access来尝试这些功能。

JEP 269 provides some convenience factory methods for Collections API. This factory methods are not in current Java version, which is 8, but are planned for Java 9 release.

For Map there are two factory methods: of and ofEntries. Using of, you can pass alternating key/value pairs. For example, in order to create a Map like {age: 27, major: cs}:

Map<String, Object> info = Map.of("age", 27, "major", "cs");

Currently there are ten overloaded versions for of, so you can create a map containing ten key/value pairs. If you don't like this limitation or alternating key/values, you can use ofEntries:

Map<String, Object> info = Map.ofEntries(
                Map.entry("age", 27),
                Map.entry("major", "cs")
);

Both of and ofEntries will return an immutable Map, so you can't change their elements after construction. You can try out these features using JDK 9 Early Access.

若无相欠,怎会相见 2024-07-20 07:05:50

嗯...我喜欢枚举;)

enum MyEnum {
    ONE   (1, "one"),
    TWO   (2, "two"),
    THREE (3, "three");

    int value;
    String name;

    MyEnum(int value, String name) {
        this.value = value;
        this.name = name;
    }

    static final Map<Integer, String> MAP = Stream.of( values() )
            .collect( Collectors.toMap( e -> e.value, e -> e.name ) );
}

Well... I like enums ;)

enum MyEnum {
    ONE   (1, "one"),
    TWO   (2, "two"),
    THREE (3, "three");

    int value;
    String name;

    MyEnum(int value, String name) {
        this.value = value;
        this.name = name;
    }

    static final Map<Integer, String> MAP = Stream.of( values() )
            .collect( Collectors.toMap( e -> e.value, e -> e.name ) );
}
如梦初醒的夏天 2024-07-20 07:05:50

我已经阅读了答案,并决定编写自己的地图生成器。 随意复制粘贴并享受。

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * A tool for easy creation of a map. Code example:<br/>
 * {@code MapBuilder.of("name", "Forrest").and("surname", "Gump").build()}
 * @param <K> key type (inferred by constructor)
 * @param <V> value type (inferred by constructor)
 * @author Vlasec (for http://stackoverflow.com/a/30345279/1977151)
 */
public class MapBuilder <K, V> {
    private Map<K, V> map = new HashMap<>();

    /** Constructor that also enters the first entry. */
    private MapBuilder(K key, V value) {
        and(key, value);
    }

    /** Factory method that creates the builder and enters the first entry. */
    public static <A, B> MapBuilder<A, B> mapOf(A key, B value) {
        return new MapBuilder<>(key, value);
    }

    /** Puts the key-value pair to the map and returns itself for method chaining */
    public MapBuilder<K, V> and(K key, V value) {
        map.put(key, value);
        return this;
    }

    /**
     * If no reference to builder is kept and both the key and value types are immutable,
     * the resulting map is immutable.
     * @return contents of MapBuilder as an unmodifiable map.
     */
    public Map<K, V> build() {
        return Collections.unmodifiableMap(map);
    }
}

编辑:最近,我经常发现公共静态方法of,而且我有点喜欢它。 我将其添加到代码中并将构造函数设为私有,从而切换到静态工厂方法模式。

EDIT2:最近,我不再喜欢名为 of 的静态方法,因为使用静态导入时它看起来很糟糕。 我将其重命名为 mapOf,使其更适合静态导入。

I've read the answers and i decided to write my own map builder. Feel free to copy-paste and enjoy.

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * A tool for easy creation of a map. Code example:<br/>
 * {@code MapBuilder.of("name", "Forrest").and("surname", "Gump").build()}
 * @param <K> key type (inferred by constructor)
 * @param <V> value type (inferred by constructor)
 * @author Vlasec (for http://stackoverflow.com/a/30345279/1977151)
 */
public class MapBuilder <K, V> {
    private Map<K, V> map = new HashMap<>();

    /** Constructor that also enters the first entry. */
    private MapBuilder(K key, V value) {
        and(key, value);
    }

    /** Factory method that creates the builder and enters the first entry. */
    public static <A, B> MapBuilder<A, B> mapOf(A key, B value) {
        return new MapBuilder<>(key, value);
    }

    /** Puts the key-value pair to the map and returns itself for method chaining */
    public MapBuilder<K, V> and(K key, V value) {
        map.put(key, value);
        return this;
    }

    /**
     * If no reference to builder is kept and both the key and value types are immutable,
     * the resulting map is immutable.
     * @return contents of MapBuilder as an unmodifiable map.
     */
    public Map<K, V> build() {
        return Collections.unmodifiableMap(map);
    }
}

EDIT: Lately, I keep finding public static method of pretty often and I kinda like it. I added it into the code and made the constructor private, thus switching to static factory method pattern.

EDIT2: Even more recently, I no longer like static method called of, as it looks pretty bad when using static imports. I renamed it to mapOf instead, making it more suitable for static imports.

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