使用条件迭代hashmap

时间:2014-08-24 11:37:40

标签: android iterator

首先,对不起我的英语。 我从android ai开始有一个链表的问题,我想在迭代器中放一个条件,但是当条件没问题时,迭代器只显示一个映射条目(hasmap有键和值,但是(和这个)很奇怪)当条件未验证时(system.out.println在if之外),它显示了hasmap的2个值????? 有谁能够帮我?。 非常感谢。

(数据是链接列表)

private void setDataBuscar(LinkedList<HashMap<String, String>> data){

        HashMap<String, String> hm = new HashMap<String, String>();        


        Iterator it = data.iterator();
        while(it.hasNext()) {

            hm = (HashMap) it.next();


            Iterator empleos = hm.entrySet().iterator();
            Map.Entry empleo;

            while (empleos.hasNext()) {
                empleo = (Map.Entry) empleos.next();


                String valor = empleo.getValue().toString();
                String llave = empleo.getKey().toString();//-->here llave have value


                system.out.println(valor+" "+llave);//--> here shows all dates(TITLE LINK)
                //  of the hasmap, without filter

                if(valor.contains(aBuscar)){
                    System.out.println(valor);
                    System.out.println(llave);//--> here dont shows llave ???
                }


              }

2 个答案:

答案 0 :(得分:0)

我将您的评论解释为您希望循环显示列表并打印当前所选实体的每个标题链接对,其中标题包含单词car(或代码aBuscar)。如果这是正确的,这是实现此行为的代码:

存储单个标题链接对的类:

public class TitleLink {

  private String title;
  private String link;

  public TitleLink() {
    this.title = "";
    this.link = "";
  }

  public TitleLink(String title) {
    this.title = title;
    this.link = "";
  }

  public TitleLink(String title, String link) {
    this.title = title;
    this.link = link;
  }

  public boolean titleContains(String title) {
    return this.title.contains(title);
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getLink() {
    return link;
  }

  public void setLink(String link) {
    this.link = link;
  }

  @Override
  public String toString() {
    return "TitleLink {title=" + title + ", link=" + link + "}";
  }
}

测试类:

public class Test {

  private static String aBuscar = "car";

  public static void main(String[] args) {
    List<TitleLink> example = new LinkedList<>();

    example.add(new TitleLink("carTitle", "firstCarLink"));
    example.add(new TitleLink("carrotTitle", "firstCarrotLink"));
    example.add(new TitleLink("bikeTitle", "bikeCarLink"));
    example.add(new TitleLink("boatTitle", "boatCarLink"));

    example.add(new TitleLink("carTitle", "secondCarLink"));
    example.add(new TitleLink("carrotTitle", "secondCarrotLink"));
    example.add(new TitleLink("bikeTitle", "secondCarLink"));
    example.add(new TitleLink("boatTitle", "secondCarLink"));

    setDataBuscar(example);
  }

  private static void setDataBuscar(List<TitleLink> data) {
    System.out.println("------");
    System.out.println(data); // -> probe ... show whole content of the map
    System.out.println("------");

    for (TitleLink titleLink : data) {
      if (titleLink.titleContains(aBuscar)) {
        System.out.println(titleLink);
      }
    }
  }

  /*
   * This version stores the current title and link in separate variable inside the loop
   * and prints them if the condition is met.
  private static void setDataBuscar(List<TitleLink> data) {
    System.out.println("------");
    System.out.println(data); // -> probe ... show whole content of the map
    System.out.println("------");

    for (TitleLink titleLink : data) {
      String title = titleLink.getTitle();
      String link = titleLink.getLink();

      if (titleLink.titleContains(aBuscar)) {
        System.out.println(title);
        System.out.println(link);
      }
    }
  }*/

}

输出:

------
// all entries
------
TitleLink {title=carTitle, link=firstCarLink}
TitleLink {title=carrotTitle, link=firstCarrotLink}
TitleLink {title=carTitle, link=secondCarLink}
TitleLink {title=carrotTitle, link=secondCarrotLink}

更改解析器类:

public class Parser {

    private URL url;
    static TitleLink entry2;

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

    public List<TitleLink> parse() {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        List<TitleLink> entries = new LinkedList<>();
        TitleLink 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 TitleLink();
                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.setTitle(property.getFirstChild().getNodeValue());
                    } else if (name.equalsIgnoreCase("link")){
                      entry.setTitle(property.getFirstChild().getNodeValue());
                      entries.add(entry);
                      entry = new TitleLink();
                    }
                }
                //entries.add(entry);
                entry2=entry;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return entries;
    }
}

我无法测试解析器类的更改,因为我没有包含此类应解析的数据的网页。如果您测试并发现任何错误,您可以尝试修复它们(不应该太难)或发表评论。

答案 1 :(得分:0)

这是解析器类Tom

public class Parser {

private URL url;
static HashMap<String, String> entry2;

public Parser(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(PortadaActivity.DATA_TITLE, property.getFirstChild().getNodeValue());
                } else if (name.equalsIgnoreCase("link")){
                    entry.put(PortadaActivity.DATA_LINK, property.getFirstChild().getNodeValue());
                }
            }
            entries.add(entry);
            entry2=entry;
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return entries;
}

}