Results 1 to 12 of 12
  1. #1
    webpler is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Question Best way to solve this? (Multidimentional array with strings as "keys")

    Hey!

    I should begin with saying i'm totally new to Java. I've programmed in PHP (and some C#) before, so i've got some experience behind my back. In PHP, you can create a multidimentional array by doing:

    $array {
    'key' => 'val',
    'another key' => 'another val'
    }

    But it appears you can't name the "keys" manually in java, at least not with strings. So what's the best way to go about this? I'm going to use it for a data access layer class that will loop through the array and use the Keys for columns, and the Values for... values. I could do this by using two different arays, one for Columns and one for Values, but i feel there must be a better solution to this.

    I've been reading about Enums, would that be a good choise?

    Regards,
    webpler

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    An object which maps keys to values is a "Map"

  3. #3
    webpler is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Question

    Thank you! It looks like using a Map object is the way to go. I tried to get it to work, but after importing java.util.Map and trying to instantiate a class, it gives me the error:

    java.util.Map is abstract; cannot be instantiated

    How do i go about using it? Some examples would be great!

    Thanks,
    webpler

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,518
    Blog Entries
    7
    Rep Power
    20

    Default

    A Map is an interface, you can't instantiate it; you have to use a HashMap or a TreeMap (implementing classes of that Map interface).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Java Code:
    All Known Implementing Classes:
        AbstractMap, Attributes, AuthProvider, ConcurrentHashMap, ConcurrentSkipListMap, EnumMap, HashMap, Hashtable, IdentityHashMap, LinkedHashMap, PrinterStateReasons, Properties, Provider, RenderingHints, SimpleBindings, TabularDataSupport, TreeMap, UIDefaults, WeakHashMap
    -->

    Java Code:
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    //....
    		Map<String, String> array = new HashMap<String, String>();
    		array.put("key", "val");
    		array.put("another key", "another val");
    		System.out.println(array.get("key")); //val
    		for (String string : array.values()) {
    			System.out.println(string);
    		}
    		for (String string : array.keySet()) {
    			System.out.println(string);
    		}
    		for (Entry<String, String> e : array.entrySet()) {
    			System.out.println(e.getKey()+" "+e.getValue());
    		}

  6. #6
    webpler is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    Oh thanks, that cleared it up a bit! There are a few different classes to choose from though, would you suggest any specific one for my usage, or am i going to have to read through all of them? (I probably will one day, but not tonight!)

    EDIT: Alright, nevermind my last question! I didn't notice eRaaaa was using a HashMap. Thanks!
    Last edited by webpler; 03-15-2011 at 08:50 PM.

  7. #7
    webpler is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    I've got one more question for you guys!

    Using a HashMap wasn't the best alternative for me because i need to be able to have several keys with the same name. I took a look at HashTable instead, but it seems to obselete? I'm not sure, but either way i can't get it to work.

    Any suggestions?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by webpler View Post
    Using a HashMap wasn't the best alternative for me because i need to be able to have several keys with the same name.
    Please elaborate on just what you mean with this statement as it's not very clear to me. Sometimes maps can hold maps, but I don't know if this helps you as I'm not yet clear on your problem.


    I took a look at HashTable instead, but it seems to obselete? I'm not sure, but either way i can't get it to work.
    I don't believe that a HashTable has any additional functionality over a HashMap. Why did you try this?

  9. #9
    webpler is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Please elaborate on just what you mean with this statement as it's not very clear to me. Sometimes maps can hold maps, but I don't know if this helps you as I'm not yet clear on your problem.
    I basically meant that i want to be able to create several mappings/rows with the same key. Like this:

    Java Code:
     valuesToInsert.put("movie_name", "This is a test");
     valuesToInsert.put("movie_name", "This is another test")
    Right now that would just overwrite the first one. I don't want that.

    Quote Originally Posted by Fubarable View Post
    I don't believe that a HashTable has any additional functionality over a HashMap. Why did you try this?
    Because i read the API for HashMap and HashTable, and in HashMap it says "If the map previously contained a mapping for the key, the old value is replaced.". It doesn't say so in the HashTable API so i figured it would allow you to store several mappings with the same key, but i was wrong.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by webpler View Post
    I basically meant that i want to be able to create several mappings/rows with the same key. Like this:

    Java Code:
     valuesToInsert.put("movie_name", "This is a test");
     valuesToInsert.put("movie_name", "This is another test")
    Right now that would just overwrite the first one. I don't want that.
    But how would you know which item is desired if both use the same key? Perhaps you want a HashMap that uses a String as a Key and holds an ArrayList of Strings, something like:

    Java Code:
    Map<String, ArrayList<String>> valuesToInsert = new HashMap<String, ArrayList<String>>();
    ArrayList<String> list = valuesToInsert.get("movie_name");
    if (list == null) {
      list = new ArrayList<String>();
      valuesToInsert.put("movie_name", list);
    }
    list.add("This is a test");
    list.add("This is another test");
    or something along these lines?

  11. #11
    webpler is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Please elaborate on just what you mean with this statement as it's not very clear to me. Sometimes maps can hold maps, but I don't know if this helps you as I'm not yet clear on your problem.


    I don't believe that a HashTable has any additional functionality over a HashMap. Why did you try this?
    Quote Originally Posted by Fubarable View Post
    But how would you know which item is desired if both use the same key? Perhaps you want a HashMap that uses a String as a Key and holds an ArrayList of Strings, something like:

    Java Code:
    Map<String, ArrayList<String>> valuesToInsert = new HashMap<String, ArrayList<String>>();
    ArrayList<String> list = valuesToInsert.get("movie_name");
    if (list == null) {
      list = new ArrayList<String>();
      valuesToInsert.put("movie_name", list);
    }
    list.add("This is a test");
    list.add("This is another test");
    or something along these lines?
    That's the thing, i don't need to know which item contains what. Not in this case. Your suggestion was pretty good though, i think that would work!

    I actually just solved this another way (i didn't create the function like i planned), but i'm still interested on how to solve it. I'm gonna give your suggestion a try, thanks!

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Using enums as keys was mentioned at one point, so don't forget EnumMap.

    There is also com.google.common.collect.MultiMap (part of Guava) - although you are even more spoilt for choice in this modern collections library!

Similar Threads

  1. Replies: 2
    Last Post: 01-24-2011, 12:26 PM
  2. How can I solve "void cannot be dereferenced" error?
    By fervent07 in forum New To Java
    Replies: 2
    Last Post: 04-05-2010, 08:54 PM
  3. How to solve this "Fixtures code" error exception?
    By makerror in forum New To Java
    Replies: 4
    Last Post: 12-02-2009, 02:45 AM
  4. Replies: 18
    Last Post: 07-12-2009, 05:47 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •