0

I have a native Android news App where I have news articles and comments under each article.

When I open the inner View(Activity) of any specific article there might be different coverages of the same article, e.g. the Coverage from CNN, BBC, FOX ...etc.

Each coverage is actually treated as a different article with Unique String ID/Hash.

My background is Perl/PHP I'd like to know how do I create something like this in java:

$array["Article1_Coverage_X"] = array($Comment1,$Comment2,$Comment3);
$array["Article1_Coverage_Y"] = array($Comment1,$Comment2,$Comment3,$Comment4);
// $Comment1 , $Comment2 ....etc are comment objects

. . i.e. the first dimension of the array is a string(key) and the 2nd dimension is of other type

I tried the following but it's not what I'm looking for:

 ArrayList<ArrayList<Comment>>  // Where Comment is a Java object with ID,Title.....etc

As the keys for both dimension are indexed integers

if there's a better approach I'd highly appreciate it. I want to store the list of comments related to each news article in a temporary variable such that it's (recycled/thrown to garbage) once the user quits the Activity, that's why I don't wanna store in an SQLite db.

1 Answer 1

1

What you need is a HashMap of ArrayLists. HashMap creates key-value pairs. Make your key as String so that you can extract with it instead of integers.

HashMap < String,ArrayList < Comment >> hmap = new HashMap < String,ArrayList < Comment >> ();

TO put in data,

hmap.put("BBC", commentlist1);

To extract,

ArrayList<Comment> list = hmap.get("CNN");

To iterate the elements, there are several ways. How to efficiently iterate over each Entry in a Map?

Also, if you want a particular order to be maintained, you may want to use LinkedHashMap

Sign up to request clarification or add additional context in comments.

2 Comments

Could you please give me an example with how to fill it and how to loop on it ?
Thanks a lot, I'll try that now, forget about looping on it, I already have keys in a separate String Array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.