Results 1 to 5 of 5
  1. #1
    kiloran is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Arrays and HashMaps

    I've used Perl a lot over the past 10 years, mainly as a procedural language to process lots of data. I'm now starting to look at Java and OOP, and inevitably having problems since my mindset is very Perl-oriented, and I have not yet reached the critical mass of Java/OOP knowledge where everything falls neatly into place..

    A couple of questions to help point me in the right direction:

    1...
    I frequently use key/value hashes in Perl, where the value is actually an array. So I can do things like:
    $data_hash{'key1'}[0] = 12;
    $data_hash{'key1'}[13] = 0.2;
    I have not (yet) found a similar structure in Java. Is it possible, or is there a better alternative structure?

    2...
    Arrays in Perl are loosely-typed, so each element of a single array can contain a string, or an integer, or a double, or boolean or whatever.
    Java appears to require that each element of an array is the same type.

    I'm looking for a structure like a multidimensional array such as
    ['SSE', '2013-01-12', 58, 0.6]
    ['LLOY', '2008-6-12', 48.2, 0]

    Any pointers to relevant documentation or examples gratefully anticipated

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Arrays and HashMaps

    1. There is the Hashtable<String, int> in Java which implements such things for you

    2. You may create an Array of generic type Object to store all kinds of objects. Note however this is not a very good approach. Any interpretation without knowing he type may be false without taking the type into account and - also in pearl - may lead to unexpected results.
    So in Java you need to cast objects before using them if you have only a generic handle. Also the ArrayList<...> might be interesting for you.
    I like likes!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arrays and HashMaps

    ad 1: a Map<String, int[]> can do the job for you:

    Java Code:
    Map<String, int[]> data_hash= new HashMap<String, int[]>;
    ...
    data_hash.put("key1'", new int[99]);
    data_hash.get("key1")[0]= 12;
    ad 2: Each type in Java has one single mother of all types: the Object class; you can (ab)use it for loosely typed objects:

    Java Code:
    Object[] array= { 1, 2.0, 3L, "four", true|false };
    ...
    int i0= (Integer)array[0];
    double i1= (Double)array[1];
    long i2= (Long)array[2];
    String i3= (String)array[3];
    boolean i4= (Boolean)array[4];
    Note that it is not the best solution for storing a hodgepodge of types in a single collection/array.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    kiloran is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Re: Arrays and HashMaps

    Many thanks for the replies. Very useful.

    For the sake of completeness, I got a compilation error for:
    Map<String, int[]> data_hash= new HashMap<String, int[]>;

    Took me a while to resolve. It should be:
    Map<String, int[]> data_hash= new HashMap<String, int[]>();

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,515
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arrays and HashMaps

    That should teach me: never type anything for a reply without testing/checking it first; sorry for the inconvenience.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Maps and HashMaps
    By moonah in forum Advanced Java
    Replies: 1
    Last Post: 01-26-2012, 03:22 PM
  2. HashMaps containing JComponents
    By chillin in forum AWT / Swing
    Replies: 3
    Last Post: 05-04-2011, 12:05 AM
  3. hashmaps
    By santa in forum New To Java
    Replies: 6
    Last Post: 05-03-2011, 11:07 PM
  4. Concerning HashMaps
    By hjensen in forum New To Java
    Replies: 7
    Last Post: 10-18-2010, 03:36 PM
  5. HashMaps get key for a specific value
    By andre1011 in forum Advanced Java
    Replies: 2
    Last Post: 03-11-2009, 02:30 AM

Posting Permissions

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