Results 1 to 4 of 4

Thread: HashMap Subkeys

  1. #1
    mpw
    mpw is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default HashMap Subkeys

    I'm new to Java - so this is probably a trivial problem, but here goes....

    I want to create a HashMap with a key / subkey / value.

    For example, to hold a shopping list - Shop / Item / Quantity. Is this possible?

    I've tried:
    Java Code:
    HashMap<String,HashMap<String,Integer>>shoppingList = new HashMap<String,HashMap<String,Integer>>;
    This compiles - but complains when I try to put anything into it.
    Java Code:
     shoppingList.put("groceries","Milk",1);
    Any suggestions very welcome.

    Thanks
    Last edited by mpw; 01-11-2009 at 11:46 PM.

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

    Default

    Let's see how do you try to put things into your maps.

    Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. Another way is to place the tag &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:

    Java Code:
    &#91;code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    &#91;/code]

  3. #3
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Not a trivial exactly, very common ....

    Create a class Item, probably have a variable how many - what is a shop?

    Then do Hashtable.add(new Key(), new Item());
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    Java Code:
    HashMap<String,HashMap<String,Integer>>shoppingList = new HashMap<String,HashMap<String,Integer>>;
    you may try
    Java Code:
    HashMap<String,Integer> items = new HashMap<String,Integer>();
    
    items.put("Milk" , 1);
    
    shoppingList.put("groceries", items);

Similar Threads

  1. Hashmap to TXT and TXT to Hashmap
    By elvinny in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2011, 12:12 AM
  2. Hashmap - get key according to value
    By gtriant in forum New To Java
    Replies: 1
    Last Post: 12-15-2008, 03:29 PM
  3. Hashmap
    By dirtycash in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 03:58 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
  •