如何创建一个静态的String Map - >排列 [英] How to create a static Map of String -> Array

查看:117
本文介绍了如何创建一个静态的String Map - >排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个静态的 Map ,它将给定的 String 映射到一个 int

I need to create a static Map which maps a given String to an array of int's.

换句话说,我想定义如下:

In other words, I'd like to define something like:

"fred" -> {1,2,5,8}
"dave" -> {5,6,8,10,11}
"bart" -> {7,22,10010}
... etc

有没有一个简单的方法在Java中执行此操作?

Is there an easy way to do this in Java?

如果可能,我想使用 static c $ c> String 和 int 值。

And if possible, I'd like to use static constants for both the String and the int values.

编辑: 为了澄清我对于价值观的 static 常量的意义,并给出我认为是正确的代码,这里是我的第一个解决方案:

To clarify what I meant by static constants for the values, and to give what I see to be the correct code, here is my first stab at the solution:

public final static String FRED_TEXT = "fred";
public final static String DAVE_TEXT = "dave";

public final static int ONE = 1;
public final static int TWO = 2;
public final static int THREE = 3;
public final static int FOUR = 4;

public final static HashMap<String, int[]> myMap = new HashMap<String, int[]>();
static {
    myMap.put(FRED_TEXT, new int[] {ONE, TWO, FOUR});
    myMap.put(DAVE_TEXT, new int[] {TWO, THREE});
}

请注意,这些名称不是我实际使用的。这只是个例子。

Note, these names are not what I'd actually be using. This is just a contrived example.

推荐答案

你不要需要分开声明和初始化。如果你知道如何,都可以在一行中完成!

You don't need to separate declaration and initialization. If you know how, it can all be done in one line!

// assumes your code declaring the constants ONE, FRED_TEXT etc is before this line
private static final Map<String, int[]> myMap = Collections.unmodifiableMap(
    new HashMap<String, int[]>() {{
        put(FRED_TEXT, new int[] {ONE, TWO, FOUR});
        put(DAVE_TEXT, new int[] {TWO, THREE});
    }});

我们在这里是一个匿名类,一个初始化块 - 在构造函数之后执行的代码块,我们在这里用于加载地图(这有时被错误地称为双括号初始化 - 我想是因为有两个相邻的大括号=但实际上没有这样的东西)。

What we have here is an anonymous class with an inialization block - a block of code that executes on construction after constructor, which we've used here to load the map (this is sometimes erroneously known as "double brace initialization" - I suppose because there's two adjacent braces = but there's actually no such thing).

这两个很酷的事情是a)它将声明与内容结合起来,b)由于初始化是内联的,您还可以对 Collections.unmodifiableMap()进行联机调用,导致一个整洁的单行声明,初始化和转换为不可修改。

The two cool things about this is a) it marries the declaration with the contents and b) because the initialization is in-line, you can also make an in-line call to Collections.unmodifiableMap(), resulting in a neat one-line declaration, initialization and conversion to unmodifiable.

如果您不需要/想要地图不可修改,请留下该调用: / p>

If you don't need/want the map to be unmodifiable, leave out that call:

private static final Map<String, int[]> myMap = new HashMap<String, int[]>() {{
    put(FRED_TEXT, new int[] {ONE, TWO, FOUR});
    put(DAVE_TEXT, new int[] {TWO, THREE});
}};

这篇关于如何创建一个静态的String Map - &gt;排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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