简体   繁体   English

Java中关联数组结构的最佳实践(与PHP相比)

[英]Best practice for associative array structure in Java (compared to PHP)

I come from a PHP background and in PHP the following structure is possible with help of associative arrays (pseudo-code): 我来自PHP背景,在PHP中,通过关联数组(伪代码)可以实现以下结构:

test["1"]["2"] = true;
test["2"]["4"] = true;

Now, i want to do the same in Java, because in PHP i can easily do the following: 现在,我想在Java中做同样的事情,因为在PHP中我可以轻松地执行以下操作:

if(test[var1][var2] == true) {
    ...
}

I like this structure, because you can easily define some constraints (ie config stuff). 我喜欢这种结构,因为你可以轻松定义一些约束(即配置东西)。 The important thing is that i need the hierarchy, that's why i use a two-dimensional array in PHP. 重要的是我需要层次结构,这就是我在PHP中使用二维数组的原因。

But when i was searching for the same thing in Java, i didn't find anything that can be achieved as easy as in PHP. 但是当我在Java中搜索相同的东西时,我没有发现任何可以像在PHP中那样容易实现的东西。 Some say i should create a hierarchy with help of classes, some others say i should deal with enums. 有人说我应该在课程的帮助下创建一个层次结构,其他人说我应该处理枚举。 I don't like both ways, because it is a lot of programming overhead for some simple definitions. 我不喜欢这两种方式,因为对于一些简单的定义来说,这是一个很多的编程开销。

Is there a simple solution in Java which can be achieved with few lines of code and does the same like the example above? Java中是否有一个简单的解决方案可以通过几行代码实现,并且像上面的示例一样? What's the best practice? 什么是最佳做法?

Hmm, you could use a map of map. 嗯,你可以使用地图地图。

Map<String, Map<String, Boolean>> map = new HashMap<String, Map<String, Boolean>>();

But I would hide this map behind a class, since the initialization is not trivial. 但是我会把这个地图隐藏在一个类后面,因为初始化并不简单。 You have to also create the inner maps. 您还必须创建内部地图。

You finally could use it like 你终于可以像使用它一样

String var1 = "1", var2 = "2"
if (map.get(var1).get(var2))
{
   /* Without proper initialization map.get(var1) could be null,
    * so it's important that you create the innermap up on first use of 'var1':
    * map.put(var1, new HashMap<String, Boolean>());
    *
    * and to associate var1 and var2 with a boolean value:
    * map.get(var1).put(var2, true);
    */
}

or even better 甚至更好

MapHider instance = new MapHider();
if (instance.check(var1, var2))
{
}

Here a generic approach and example of use 这是一个通用的方法和使用示例

/**
 * AssociativeArray2D - Two dimensional associative array
 *
 * NOTE: Not thread-safe.
 *
 * @param <K1> type of first key
 * @param <K2> type of second key
 * @param <V> type of value
 */
public class AssociativeArray2D<K1, K2, V> {

    /* standard value if no value is in the map */
    private final V standard;
    private Map<K1, Map<K2, V>> map = new HashMap<K1, Map<K2, V>>();

    public AssociativeArray2D(V standard) {
        this.standard = standard;
}

    public static AssociativeArray2D<String, String, Boolean> getSSB() {
        return new AssociativeArray2D<String, String, Boolean>(false);
}

    public void add(K1 first, K2 second, V value) {
        Map<K2, V> m = map.get(first);
        if (m == null) {
            m = new HashMap<K2, V>();
            map.put(first, m);
        }
        m.put(second, value);
    }

    public V check(K1 first, K2 second) {
        Map<K2, V> m = map.get(first);
        if (m == null) {
            m = new HashMap<K2, V>();
            map.put(first, m);
            m.put(second, standard);
        }
        return m.get(second);
    }

    public static void main(String[] args) {
        AssociativeArray2D<String, String, Boolean> a = AssociativeArray2D.getSSB();

        if (a.check("1", "2")) {
            System.out.println("This, won't print!");
        }

        a.add("1", "2", true);

        if (a.check("1", "2")) {
            System.out.println("This, will print!");
        }
    }   
}

OUTPUT OUTPUT

This, will print! 这个,会打印!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM