1

I am fairly new in Java, so I have to rely on this community for this one.

I need to store an object in some sort of array/list where I can quickly access the object using a string and two interger keys. Something like a["string"][1][1] - I have looked over some different guides and tutorials, but not been able to come up with a good solution that's easy to manage.

I am creating a Minecraft plugin where I need to keep track of where specific Blocks are with world, chunk_x, and chunk_z- I am trying to create a method where I can provide a location, which has the three beforementioned values, and do a quick look up based on the world and chunk, so I do not have to iterate all stored blocks in the world, but can limit it to 9 chunks of the world. (Current chunk i am in and all surrounding neighbours)

2
  • You might want to give some more background on what you're trying to do. Why do you need three different keys to point to the same object? That's an unusual requirement, so if you explain what you're trying to accomplish, people can check your thinking. Commented May 6, 2020 at 20:28
  • I have updated the question. Thank you. Commented May 6, 2020 at 20:34

3 Answers 3

2

How about this:

Map<String, Object[][]> store;
Sign up to request clarification or add additional context in comments.

Comments

1

Does it have to be a multidimensional array? You could use just a hash map with a custom key that holds your string key and the two integer keys. Here is a complete example of what I mean:

import java.util.HashMap;
import java.util.Objects;

public class multidim {
    static class Key {
        int index0, index1;
        String str;
        int _hash_code;

        public Key(String s, int i0, int i1) {
            _hash_code = Objects.hash(s, i0, i1);
            str = s;
            index0 = i0;
            index1 = i1;
        }

        public int hashCode() {
            return _hash_code;
        }

        public boolean equals(Object x) {
            if (this == x) {
                return true;
            } else if (x == null) {
                return false;
            } else if (!(x instanceof Key)) {
                return false;
            }
            Key k = (Key)x;
            return (index0 == k.index0)
                && (index1 == k.index1)
                && Objects.equals(str, k.str);
        }
    }

    public static void main(String[] args) {
        HashMap<Key, Double> m = new HashMap<Key, Double>();
        m.put(new Key("mjao", 3, 4), 119.0);
        m.put(new Key("katt$k1t", 4, 6), 120.0);

        System.out.println("Value that we put before: "
            + m.get(new Key("mjao", 3, 4)));
    }
}

We define a class Key that represents the values you use to access elements and we override its equals and hashCode methods so that it can be used in a hash map. Then we just use it with the java.util.HashMap class. Running the above program will output Value that we put before: 119.0.

Edit: Add this == x comparison in equals (a small optimization).

2 Comments

I went with this as an answer. Implemented it, it works, so seems like the solution I am looking for. Do you know about how this performs compared to other suggestions in this thread?
@SeverinDK How it performs? Looking up a value in the hash map is O(1) time complexity I believe (which is the best you will get with other answers using Map too). There is a little bit of overhead (unless the JIT optimizes it away) in allocating the Key instance.The big advantage of this approach is that you don't need to allocate a big 2D array upfront and there are no limitiations on the bounds of the indices. You only pay for the entries that you access. So this approach is arguably simple and time complexity is good.
1

What about combination of Map and Pair?

Map<String, Pair<Integer, Integer>> tripletMap = new HashMap<>;

tripletMap.put(Pair.with(23, 1););

You can access values from your triplet as any map and then retrieved Pair as:

Pair<Integer, Integer> myPair = tripletMap.get("key")
myPair.getValue0()
myPair.getValue1()

2 Comments

Here are a few guides to help you get more familiarized with the concept since it is not something that is supported by the Java core libraries. baeldung.com/java-tuples geeksforgeeks.org/triplet-class-in-java-tuples
Edited with Map+Pair usage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.