简体   繁体   English

如何获取LinkedList的值 <HashMap<String,String> &gt;?

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

I have this XMLParser: 我有这个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. 问题是我不知道如何获取LinkedList上添加的最后一个值并将其显示在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. 显然,我没有足够的经验,并且我对Map,HashMap等等一无所知。XMLParser不是我的。

For iterating through all the values in your HashMap, you can use an EntrySet: 要遍历HashMap中的所有值,可以使用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. 因此,该条目是HashMap,这意味着您需要从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来说,有一个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 

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. 您还可以通过Google搜索“ Java Map示例”了解更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在HashMap中查找键 <String, LinkedList<String> &gt;在LinkedList中具有一个或多个相似值的&gt; <String> - How to find keys in the HashMap<String, LinkedList<String>> which have one or more similar values in LinkedList<String> 添加到HashMap中的linkedList <String, LinkedList> - Adding to a linkedList in a HashMap<String, LinkedList> 我正在尝试遍历Hashmap <LinkedList<String> ,int [] &gt;&gt;。 有比我正在做的更简单的方法吗? - I am trying to iterate through a Hashmap<LinkedList<String>, int[]>>. Is there a simpler way to do it than what I am doing? 如何将字符串存储到哈希图中没有名称的新String []中? - How do I store a string into a new String[] that has no name in a hashmap? 如何在Hashmap中的字符串数组中访问各个值? - How do I access individual values within a string array within a Hashmap? 我有两个 hashmap HashMap <string,list<string> &gt; 在这种格式下,如何比较两个值是否相同或不同的键</string,list<string> - I have two hashmap HashMap<String,List<String>> in this format how to compare both the values are same or not with same keys 如何在hashmap中存储值 <String, List<Integer> &gt; - How can I store values in a hashmap with <String, List<Integer>> 如何使用HashMap值形成字符串? - How can I use HashMap values to form a String? JAVA:HashMap <String, ArrayList> - 我如何关联这些值? - JAVA: HashMap<String, ArrayList> - How can I associate the values? 如何对LinkedList <String>进行排序? - How to sort LinkedList<String>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM