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!!!
Re: How to create and use Hashtable, ArrayList (really urgent, please)
You have the right idea if you want to use a HashMap:
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.
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.
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.
Code:
ArrayList<String> bookList = new ArrayList<String>();
bookList .add("Book1");
bookList .add("Book2"):
.......
Then, simply add an entry into the HashMap for Books:
Code:
categoryMap.put("Book",bookList);
Make sense?
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?
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!