Multidimensional hashmap or something alse array related.

Posted on 2005-05-16
Last Modified: 2008-01-09
I need something in Java that would allow me to set multidimentional arrays with the ability to set String values as both as keys  and as the value.  Also, I need to be able to check if the keys exists within one and two dimentions.  Something like.

myPlan[0][0] = "cell 1"
myPlan[0][1] = "cell 2"
myPlan[0][2] = "cell 3"
myPlan[1][0] = "cell 4"
myPlan[1][1] = "cell 5"
myPlan[1][2] = "cell 6"

than I need to be able to check if centerain keys exist for the first set/dimention, for example myPlan.containsKey(0), as well as for both dimentions myPlan.containsKey(0)(2).  I don't want to check for values it has to check if the keys exist, which at times may be Strings.

I'm not sure if HashMap is even the correct thing to use in this instance and I'm not familiar with Vectors to start figuring it out on my own.  

Thanks for your help and recommenations.
Question by:kasiencja

    Author Comment

    Hmm, I just looked over the code again and it looks like I might be able to get away with the key values being set to int only.
    LVL 13

    Accepted Solution

    Hi kasiencja,

    u can still use a single Hastable object, but the key will be a Class u will define (Key2D) that implements equals() and hashCode() Methods, this will be the Key and the Value will  be the string:

    public class Key2D {
     int xCordinate;
     int yCordinate;

     public boolean equals(object) {
       if(object instanceof Key2D) {
        // cast object to Key2D and compare x and y

     public int hashCode() {
        //Convert x, y to Strings and concatinate them with space badding based on maximum lenght and then return the hashCode of the result String
       return concatentaionResult.hashCode();

    now u can search the Hashtable using hashtable variable.containsKey(instanceOfKey2D)

    u can also use any Map based object instead of Hashtable like HashMap or TreeMap, etc.....
    LVL 92

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    fix34  challenge 9 70
    count8 challlenge 13 65
    changePi Challenge 15 55
    nestparen challenge 4 33
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    This video teaches viewers about errors in exception handling.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now