Results 1 to 5 of 5

Thread: HashMap usage

  1. #1
    vasavi.singh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default HashMap usage

    hi,

    I have a doubt in HashMap usage. I am having a program in which HashMap is declared and few elements are added to it and based on the key it is giving the value stored in that key. While compiling it is giving the error at the declaration of HashMap. I referred complete reference as well as in net also. The syntax specified in in references is same as What syntax used in the program. But it is giving that ": is expected".

    can u tell me how to declare a HashMap in a program.

    <code>
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;

    public final class TestHashMap
    {
    public static void main( String[] args )
    {
    HashMap<String, String> h = new HashMap<String, String>(149,0.75f);

    h.put( "WA", "Washington" );
    h.put( "NY", "New York" );
    h.put( "RI", "Rhode Island" );
    h.put( "BC", "British Columbia" );

    String stateName = h.get( "NY" );

    System.out.println( stateName );

    System.out.println( "enumerate all the keys in the HashMap" );
    }

    }
    </code>

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

    Default

    That code looks OK - perhaps you could give the exact compiler message and say which line it is referring to.

  3. #3
    vasavi.singh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default HashMap usage

    TestHashMap.java:11: error: ':' expected.
    HashMap<String, String> h = new HashMap<String, String>(149,0.75f);
    ^
    1 error

    at the point of declaration it is giving the error.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    That code compiles and runs fine (from DOS command prompt).

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

Similar Threads

  1. usage of Hashtable
    By venu2807 in forum New To Java
    Replies: 4
    Last Post: 01-27-2009, 04:36 AM
  2. Usage of HTTPS
    By Sayed in forum Advanced Java
    Replies: 8
    Last Post: 12-06-2008, 11:02 AM
  3. Need help with AST usage on Eclipse! Thanks!
    By j_aquino314 in forum Eclipse
    Replies: 0
    Last Post: 10-08-2008, 08:15 PM
  4. Integer Usage
    By bluegreen7hi in forum New To Java
    Replies: 11
    Last Post: 01-24-2008, 09:54 AM
  5. Comm API Usage
    By hobbyist in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 05:59 PM

Posting Permissions

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