1 Reply Latest reply: Feb 1, 2009 9:10 AM by 843785 RSS

    while loop in a Hash Map

    843785
      My while loop doesnt seem to work in a hash map, it works fine when I loop an array list.
      It compiles but it doesnt seem to find any employees, should I use another loop?
      {code
      public Employee find(String id)
      {
      Employee foundEmployee = null;
      int index = 0;
      boolean found = false;
      while(index < register.size() && !found){
      Employee right = register.get(index);
      String namn = right.getName();
      if (namn.equals(id)){
      foundEmployee = right;
      found = true;
      }
      index++;
      }
      return foundEmployee;
      }}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
        • 1. Re: while loop in a Hash Map
          843785
          what if you looped on the key set?
            public Employee find(String id)
            {
              Employee foundEmployee = null;
              
              Set<Integer> keySet = register.keySet();
              for (Integer key : keySet)
              {
                Employee right = register.get(key);
                if (right.getName().equals(id))
                {
                  foundEmployee = right;
                  break;
                }
              }
              return foundEmployee;
            }