Results 1 to 6 of 6
  1. #1
    Python is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Exclamation How to create and use Hashtable, ArrayList (really urgent, please)

    Hi guys,

    I'm new to Java, and little bit confused with arrays. It's easy to do it in PHP, but it is different in Java.

    I need to create an array, like the one listed below, and print it using "for" loops. Could you please help me with that. Thanks in advance.


    myCatsAndSubcats = array(
    "Books" => array(
    "Book1", "Book2", "Book3"
    ),
    "Food" => array(
    "Food1", "Food2", "Food3"
    ),
    "Medical" => array(
    "Pills1", "Pills2", "Pills3"
    ),
    );


    I guess I need something like Hashtable<String, ArrayList<String>> myCategories = new Hashtable<String, ArrayList<String>>();

    So now I need to add several categories(to be precise, "Books", "Food", "Medical"), and for each particular category I need to add the list of its subcategories.

    Thank you very much!!!

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: How to create and use Hashtable, ArrayList (really urgent, please)

    You have the right idea if you want to use a HashMap:

    Java Code:
    HashMap<String, ArrayList<String>> categoryMap = new HashMap<String, ArrayList<String>>();
    Then, to add entries to the map, you would use the put(key, value) method.

  3. #3
    Python is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: How to create and use Hashtable, ArrayList (really urgent, please)

    Thanks, sehudson.

    Will try to understand hashmap thing.

    I'm not familiar to any of these... Imported
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Hashtable;
    import java.util.List;

    and still getting error for put method.

    Do I need to fill categories first, with elements (Book, Food, Medicals), and than use put() method for each one?

    categoryMap.put("Book", "Book1");
    categoryMap.put("Book", "Book2");
    categoryMap.put("Book", "Book3");

    categoryMap.put("Food", "Food1");
    etc.

    How do I actually add elements to categories?

    I feel like a dummy in Java.

    That's why I just need a working example (an array, mentioned above as well as its printout), so I can review the code, understand what exactly does that code and write my own, with changes.

    Tries to find some examples for my particular problem, but no luck.

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: How to create and use Hashtable, ArrayList (really urgent, please)

    The HashMap example I gave you is setup such that the key is a String, and the Value is an ArrayList of Strings.
    So, to add your books, you would need to first create the arraylist, add books to it, then add an entry into the map for books.

    Java Code:
    ArrayList<String> bookList = new ArrayList<String>();
    bookList .add("Book1");
    bookList .add("Book2"):
    .......

    Then, simply add an entry into the HashMap for Books:

    Java Code:
    categoryMap.put("Book",bookList);
    Make sense?

  5. #5
    Python is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: How to create and use Hashtable, ArrayList (really urgent, please)

    YES!!!
    You're helping me a lot.

    So what I have now is:


    HashMap<String, ArrayList<String>> categoryMap = new HashMap<String, ArrayList<String>>();

    ArrayList<String> bookList = new ArrayList<String>();
    bookList .add("Book1");
    bookList .add("Book2");
    bookList .add("Book3");

    ArrayList<String> foodList = new ArrayList<String>();
    foodList .add("Food1");
    foodList .add("Food2");
    foodList .add("Food3");

    ArrayList<String> medicalsList = new ArrayList<String>();
    medicalsList .add("Pill1");
    medicalsList .add("Pill2");
    medicalsList .add("Pill3");

    categoryMap.put("Book", bookList);
    categoryMap.put("Food", foodList);
    categoryMap.put("Medical", medicalsList);


    System.out.println("Number of categories: " + categoryMap.size()); // just printed the number of elements


    Now I need to check if, for example, "Food2" exists in one of subcategories.
    So I need to go through each category, and check, if "Food2" exists in the list of subcategories for each category.
    Sure, I need to have loops.

    Probably something like this:

    String myString = "Food2";

    for(String k : categoryMap.keySet())
    {
    for (ArrayList<String> v : categoryMap.values())
    {
    if (v.equals(myString))
    System.out.println("Exists");
    else
    System.out.println("NotExists");
    }
    }

    But I thing I did something wrong.

    Could you please help me with this too?

  6. #6
    Python is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: How to create and use Hashtable, ArrayList (really urgent, please)

    Seems I did it and it works.

    String myString = "Food2";

    boolean exists = false;

    for(String k : categoryMap.keySet())
    {
    if (categoryMap.get(k).contains(myString))
    exists = true;

    }


    I appreciate your help!
    Thank you very much!

Similar Threads

  1. Hashtable question. Getting info from a hashtable
    By sindrem in forum New To Java
    Replies: 4
    Last Post: 01-19-2012, 05:00 PM
  2. Replies: 20
    Last Post: 01-03-2012, 05:15 PM
  3. Is it possible to create a 2D arraylist?
    By pahiker in forum New To Java
    Replies: 12
    Last Post: 06-15-2010, 05:49 PM
  4. Trying to create an undo function using an arraylist.
    By Spirit356 in forum New To Java
    Replies: 6
    Last Post: 04-27-2010, 07:01 PM
  5. Create UI based on HashTable
    By zoe in forum AWT / Swing
    Replies: 1
    Last Post: 08-01-2007, 11:59 PM

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
  •