Android 使用条件迭代HashMap

Android 使用条件迭代HashMap,android,data-structures,iterator,Android,Data Structures,Iterator,首先,对不起我的英语 我从Android开始,但我对LinkedList有一个问题。我想在迭代器中放置一个条件,但当条件为OK时,迭代器只显示一个映射条目(HashMap有key和value,但(这很奇怪)当条件未验证(system.out.println在if之外)时,它显示HashMap的2个值 有人能帮我吗 非常感谢 (数据是一个链接列表) private void setDataBuscar(链接列表数据){ HashMap hm=新的HashMap(); Iterator it=dat

首先,对不起我的英语

我从Android开始,但我对LinkedList有一个问题。我想在迭代器中放置一个条件,但当条件为OK时,迭代器只显示一个映射条目(HashMap有key和value,但(这很奇怪)当条件未验证(system.out.println在if之外)时,它显示HashMap的2个值

有人能帮我吗

非常感谢

(数据是一个链接列表)

private void setDataBuscar(链接列表数据){
HashMap hm=新的HashMap();
Iterator it=data.Iterator();
while(it.hasNext()){
hm=(HashMap)it.next();
迭代器empleos=hm.entrySet().Iterator();
地图.入境就业;
while(empleos.hasNext()){
empleo=(Map.Entry)empleos.next();
字符串valor=empleo.getValue().toString();
字符串llave=empleo.getKey().toString();//-->这里llave有值
system.out.println(valor+“”+llave);//-->此处显示所有日期(标题链接)
//不带过滤器的hasmap的
如果(英勇包含(阿布斯卡)){
系统输出打印LN(valor);
System.out.println(llave);//-->这里不显示llave???
}
}

我理解您的评论,即您喜欢在列表上循环并打印当前选定实体的每个标题链接对,其中标题包含单词
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);
      }
    }
  }*/

}
更改语法分析器类:

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;
    }
}
公共类解析器{
私有URL;
静态标题链接入口2;
公共解析器(字符串url){
试一试{
this.url=新url(url);
}捕获(格式错误){
e、 printStackTrace();
}
}
公共列表解析(){
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
列表条目=新建LinkedList();
标题链接条目;
试一试{
DocumentBuilder=factory.newDocumentBuilder();
文档dom=builder.parse(this.url.openConnection().getInputStream());
Element root=dom.getDocumentElement();
nodelistitems=root.getElementsByTagName(“项”);

对于(inti=0;i这是解析器类Tom

公共类解析器{

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;
}
私有URL;
静态HashMap入口2;
公共解析器(字符串url){
试一试{
this.url=新url(url);
}捕获(格式错误){
e、 printStackTrace();
}
}
公共链接列表解析(){
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
LinkedList条目=新建LinkedList();
HashMap条目;
试一试{
DocumentBuilder=factory.newDocumentBuilder();
文档dom=builder.parse(this.url.openConnection().getInputStream());
Element root=dom.getDocumentElement();
nodelistitems=root.getElementsByTagName(“项”);

对于(int i=0;i您对
是什么意思,但当条件未验证时(system.out.println在if之外)
?您的意思是如果删除
if(valor.contains(aBuscar))
检查,然后两个条目都打印出来?这有什么奇怪的?你想要打印任何数据而不进行任何过滤程序会这样做吗?请重写你的问题以澄清你的问题。嗨,Jens,我会用一个例子,可能更好,我很难用英语解释。hasmap有一个例子(汽车、轮胎)..很多类似这样的情况,如果我试图只显示包含“car”的条目,那么在迭代器中会放置一个条件if(map.getkey.contains(“car”){Toast.make..“key”+map.getkey+“values”+map.getvalue,它应该显示“car tire”,但只显示“car”。如果我在没有条件的情况下探索如何正确显示所有值。首先感谢您的努力和时间。我尝试了您的类,但无法实现它们,因为应用程序的其他功能停止工作。但我将它们保留在从一开始就开始的应用程序中,以备将来使用。还有一件事:)…如果LinkedList通过执行“while empleos.hasNext()”来列出每个步骤显示一个数字,首先是一个,然后是一个链接标题,如果不是,那么在中,因为一次只有一件事。如果你把计数器放在里面,while就会看到。有没有一种方法,如果它满足条件,还会将以下内容保存在两个不同的字符串变量中?如果你不能花更多的时间,这个学习者会理解并将其作为解决方案验证您提供给我的,如果有人可以使用它。我已经厌倦了尝试使用这种方法。非常感谢和问候。我已经更新了一点答案。我添加了另一个版本的
setDataBuscar
方法。这些局部变量用于存储标题和链接。请检查这是否与您匹配期待。不用担心,我仍然想帮助你:)。最后!!!!,我修改了我的主代码并使用了你的TitleLink类,我的主要问题已经解决了。现在我必须解决一些小问题,但我认为我能解决它,(我希望:)。非常感谢你的耐心,并确保你能在这个论坛上再次见到我(jeje)。
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;
    }
}
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;
}