如何获取LinkedList的值<hashmap <string,string>&gt;?</hashmap <string,string>

时间:2014-02-26 21:36:17

标签: java android map linked-list hashmap

我有这个XMLParser:

public class XMLParser {
    private URL url;

    public XMLParser(String url){
        try {
            this.url = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    public LinkedList<HashMap<String, String>> parse() {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        LinkedList<HashMap<String, String>> entries = new LinkedList<HashMap<String,String>>();
        HashMap<String, String> entry;
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document dom = builder.parse(this.url.openConnection().getInputStream());
            Element root = dom.getDocumentElement();
            NodeList items = root.getElementsByTagName("item");
            for (int i=0; i<items.getLength(); i++) {
                entry = new HashMap<String, String>();
                Node item = items.item(i);
                NodeList properties = item.getChildNodes();
                for (int j=0; j<properties.getLength(); j++) {
                    Node property = properties.item(j);
                    String name = property.getNodeName();
                    if (name.equalsIgnoreCase("title")) {
                        entry.put(CheckNewPost.DATA_TITLE, property.getFirstChild().getNodeValue());
                    } else if (name.equalsIgnoreCase("link")) {
                        entry.put(CheckNewPost.DATA_LINK, property.getFirstChild().getNodeValue());
                    }
                }
                entries.add(entry);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return entries;
    }

}

问题是我不知道如何获取LinkedList上的最后一个值并在Toast上显示它。我试过这个:

XMLParser par = new XMLParser(feedUrl);
        HashMap<String, String> entry = par.parse().getLast();
        String[] text = new String[] {entry.get(DATA_TITLE), entry.get(DATA_LINK)};
        Toast t = Toast.makeText(getApplicationContext(), text[0], Toast.LENGTH_LONG);
        t.show();

显然我没有足够的经验,我对Map,HashMap几乎一无所知...... XMLParser不是我的。

2 个答案:

答案 0 :(得分:0)

要迭代HashMap中的所有值,可以使用EntrySet:

for (Map.Entry<String, String> entry : hashMap.entrySet()) {
    Log.i(TAG, "Key: " + entry.getKey() + ", Value: " + entry.getValue());
}

并且只获取最后一个值,您可以尝试:

hashMap.get(hashMap.size() - 1);

答案 1 :(得分:0)

因此,条目是HashMap,这意味着您需要从Map中获取Named / Value对。因此对于map,Named也称为Key或(哈希索引),并且对于每个“Key”,只有一个“值”,所以。

Map<String, String> entries = new HashMap<String, String>();
entries.put("Key1", "Value for Key 1");
entries.put("Key 2", "Key 2 value!");
entries.put("Key1", "Not the original value for sure!"); // replaced 

以后你可以得到它:

String val1 = entries.get("Key1"); // "Not the original value for sure!"
String val2 = entries.get("Key 2"); //"Key 2 value!"

并循环浏览地图:

for (String key : entries.keySet()) {
   String value = entries.get(key);
}

希望有所帮助。您还可以谷歌“Java地图示例”了解更多信息。