2

How can I get an array of elements in a ArrayList of HashMaps? I have an HashMap with url key on it. The value is a url address. Several HashMaps are stored in a ArrayList. What I want is an array with all url strings. I'm not happy with the solution I found because I think it could be extracted from ArrayList with some manipulation.

 // Hashmap for ListView 
 ArrayList<HashMap<String, String>> itemsList = new ArrayList<HashMap<String, String>>();
 // Creating JSON Parser instance
 JSONParser jParser = new JSONParser();
 jParser.execute(url); 
 try {
 JSONObject json = jParser.get();
 items = json.getJSONArray(TAG_ITEMS);
 //This is the solution that I want to optimize
 urls = new String[items.length()];
 // looping through All items
 for(int i = 0; i < items.length(); i++){
 JSONObject c = items.getJSONObject(i);
 // Storing each json item in variable
 String title = c.getString(TAG_TITLE);
 String description = c.getString(TAG_DESCRIPTION);
 String author = c.getString(TAG_AUTHOR);
 // Media is another JSONObject
 JSONObject m = c.getJSONObject(TAG_MEDIA);
 String url = m.getString(TAG_URL);
 // creating new HashMap
 HashMap<String, String> map = new HashMap<String, String>();
 // adding each child node to HashMap key => value
 map.put(TAG_TITLE, title);
 map.put(TAG_DESCRIPTION, description);
 map.put(TAG_AUTHOR, author);
 map.put(TAG_URL, url);
 // Solution
 urls[i] = url;
 // adding HashList to ArrayList
 itemsList.add(map);
 }
 } catch (JSONException e) {
 e.printStackTrace();
 } catch (InterruptedException e) {
 e.printStackTrace();
 } catch (ExecutionException e) {
 e.printStackTrace();
 }
giannis christofakis
8,3424 gold badges56 silver badges67 bronze badges
asked Nov 27, 2012 at 22:33

2 Answers 2

4

From what I can deduce from your question, it sounds like you're trying to do the following

// Assuming previously declared and instantiated urls ArrayList with populated values in the nested HashMaps. 
ArrayList<HashMap<String, String>> urls;
// Create a new List using HashMap.values from urls.
List<String> urlList = new ArrayList<String>(urls.get(index).values());

urls.get(index).values() will return a Collection view of the values contained in the HashMap at the specified ArrayList index, which will be used to instantiate and populate a new ArrayList.

If you want to obtain all of the values within each of the nested HashMaps or urls, you can do so in a similar manner, but you will need to iterate through the entire urls ArrayList

for (HashMap<String, String> urlValues : urls) {
 urlList.addAll(urlValues.values());
}

P.S. Forgive my bad variable names!

answered Nov 28, 2012 at 1:08

Comments

0

It looks like you are building a table. If you can add the Google Guava Library, you could just use the following:

Table<Integer, String, String> Table = HashedBasedTable.create()

See the JavaDoc:

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Table.html

answered Nov 27, 2012 at 22:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.