java HashMap 元素到 Array 的 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13594400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:20:35  来源:igfitidea点击:

ArrayList of HashMap elements to Array

javaandroidarraysarraylisthashmap

提问by Medeiros

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

如何获取 a ArrayListof 中的元素数组HashMaps?我有一个带有 url 键的 HashMap。该值是一个 url 地址。有几个HashMaps存储在一个ArrayList. 我想要的是array包含所有 url 字符串的。我对找到的解决方案不满意,因为我认为可以通过ArrayList一些操作来提取它。

    // 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();
    }

回答by Cooper

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 Collectionview of the values contained in the HashMapat the specified ArrayListindex, which will be used to instantiate and populate a new ArrayList.

urls.get(index).values()将返回Collection包含在HashMap指定ArrayList索引处的值的视图,该视图将用于实例化和填充新的ArrayList.

If you want to obtain allof the values within eachof the nested HashMapsor urls, you can do so in a similar manner, but you will need to iterate through the entire urls ArrayList

如果您想获取每个嵌套or 中的所有值,您可以以类似的方式进行,但您需要遍历整个HashMapsurlsurls ArrayList

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

P.S. Forgive my bad variable names!

PS 原谅我糟糕的变量名!

回答by CaTalyst.X

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:

请参阅 JavaDoc:

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

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