Search code examples
androiddata-structuresiterator

Iterating a HashMap with a condition


First, sorry for my English.

I'm starting with Android and I have a problem with a LinkedList. I want to put a condition inside the iterator, but when the condition is OK, the iterator shows only one map entry (the HashMap have key and value <String, String>, but (and this is curious) when the condition not validate (the system.out.println is outside the if) it's shows 2 values of the HashMap.

Can anybody help me?

Many thanks.

(data is a LinkedList)

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 ???
                }
                
                        
              }

Solution

  • I interpret your comment that you like to loop over the list and print every title-link pair of the currently selected entity where the title contains the word car (or in your code aBuscar). If this is correct, this is the code to achieve this behavior:

    Class that stores single title-link pair:

    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 + "}";
      }
    }
    

    Test class:

    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);
          }
        }
      }*/
    
    }
    

    Output:

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

    The changes parser class:

    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;
        }
    }
    

    I couldn't test the changes of the parser class, because I don't have the webpage that contains the data this class should parse. If you test it and find any bug, you can try to fix them (shouldn't be too hard) or leave a comment.