Chris2015
Chris2015

Reputation: 115

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):

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:

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

I like this structure, because you can easily define some constraints (i.e. config stuff). The important thing is that i need the hierarchy, that's why i use a two-dimensional array in 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. 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? What's the best practice?

Upvotes: 1

Views: 587

Answers (1)

mike
mike

Reputation: 5055

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

This, will print!

Upvotes: 4

Related Questions