简体   繁体   中英

How do I obtain the values of LinkedList<HashMap<String,String>>?

I have this 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;
    }

}

The problem is that I don't know how to obtain last value added on LinkedList and show it on a Toast. I tried this:

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

Evidently I don't have sufficient experience and I know almost nothing about Map, HashMap or so… The XMLParser is not mine.

For iterating through all the values in your HashMap, you can use an EntrySet:

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

And for only obtaining the last value, you can try this:

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

So the entry is HashMap that mean you need to get the Named/Value pairs out of the Map. So for map, there is Named also known as Key or (hashed index), and for each "Key" there exist exactly one "value" so that.

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 

and later you can get it as:

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

and to loop through the Map you would do:

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

Hope that help. You can also google "Java Map example" for more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM