【问题标题】:looping through iterator returns the same value遍历迭代器返回相同的值
【发布时间】:2026-01-08 15:15:02
【问题描述】:

我的循环没有中断并返回相同的值的问题

  public Map<String, String> getKeysByValue(Map<String, Map<String, Peple>> map, Collection<Peple> value) {

    Map<String, String> stringStringMap = new HashMap<>();

    int count = 0;

    while (value.iterator().hasNext()) {
        for (Map.Entry<String, Map<String, Peple>> entry : map.entrySet()) {
            for (Map.Entry<String, Peple> entry1 : entry.getValue().entrySet()) {
                String verified = value.iterator().next().gerVerfied();
                if (verified.equals("true")) { 
                    stringStringMap.put(entry1.getKey(), value.iterator().next().getName());

                }
            }
    }

这里的问题是,为每个键(value.iterator().next().getName())放入映射中的相同值总是返回相同的字符串

【问题讨论】:

    标签: java loops for-loop iterator


    【解决方案1】:

    此代码适用于以下输入:

          import java.util.*;
    
       public class Test {
       public static  void main(String[] ar){
        Map<String, People> entry1 = new HashMap<String , People>();
        People people1 = new People("true","name1");
        People people2 = new People("false","name2");
        People people3 = new People("true","name3");
        entry1.put("user1", people1);
        entry1.put("user2", people2);
        entry1.put("user3", people3);
    
        Map<String, People> entry2 = new HashMap<String , People>();
        People people4 = new People("true","name1");
        People people5 = new People("false","name2");
        People people6 = new People("true","name3");
        entry2.put("user1", people4);
        entry2.put("user2", people5);
        entry2.put("user3", people6);
    
        Map<String, Map<String, People>> map2 = new HashMap<String, Map<String, 
       People>>();
        map2.put("set1",entry1);
        map2.put("set2",entry2);
    
        Collection<People> strings = new ArrayList<>();
        strings.add(people1);
        strings.add(people6);
        strings.add(people5);
        strings.add(people3);
        strings.add(people2);
    
        getKeysByValue(map2, strings);
    
    }
    
    public static Map<String, String> getKeysByValue(Map<String, Map<String, People>> map, Collection<People> value) {
    
        Map<String, String> stringStringMap = new HashMap<>();
    
        int count = 0;
        Iterator<People> it = value.iterator();
    
        while (it.hasNext()) {
            People people = it.next();
            for (Map.Entry<String, Map<String, People>> entry : map.entrySet()) {
                for (Map.Entry<String, People> entry1 : entry.getValue().entrySet()) {
    
                    String verified = people.getVerified();
                    System.out.println(verified);
                    System.out.println("Key : "+entry1.getKey() +" Value : "+entry1.getValue().getVerified());
                    if (verified.equals(entry1.getValue().getVerified())) {
                       stringStringMap.put(entry1.getKey(), people.getName());
                    }
                }
            }
        }
        System.out.println(stringStringMap);
        return stringStringMap;
       }
      }
    

    People.java

      public class People {
    String verified;
    
    public  People(String verified, String name){
        this.verified = verified;
        this.name = name;
    }
    
    public String getVerified() {
        return verified;
    }
    
    public String getName() {
        return name;
    }
    String name;
    

    }

    【讨论】:

      【解决方案2】:

      我认为以下方法可以解决您的问题:

      public Map<String, String> getKeysByValue(Map<String, Map<String, Peple>> map, Collection<Peple> value) {
      
                  Map<String, String> stringStringMap = new HashMap<>();
      
                  int count = 0;
      
                  Iterator<People> peopleIterator = value.iterator();
      
                  while (peopleIterator.hasNext()) {
                      for (Map.Entry<String, Map<String, Peple>> entry : map.entrySet()) {
                          for (Map.Entry<String, Peple> entry1 : entry.getValue().entrySet()) {
                              People people = peopleIterator.next();
                              String verified = people.gerVerfied();
                              if (verified.equals("true")) {
                                  stringStringMap.put(entry1.getKey(), people.getName());
      
                              }
                          }
                      }
      

      您需要将迭代器存储在一个变量中。

      考虑下面的简单代码:

      Collection<String> strings = new ArrayList<>();
              strings.add("value1");
              strings.add("value2");
              strings.add("value3");
              strings.add("value4");
      
              while (strings.iterator.hasNext())
              {
                  System.out.println(strings.iterator.next());
              }
      

      这将无限运行并且只会打印 value1,但如果你修改代码如下:

      Collection<String> strings = new ArrayList<>();
      strings.add("value1");
                  strings.add("value2");
                  strings.add("value3");
                  strings.add("value4");
      
              Iterator<String> stringIterator = strings.iterator();
      
              while (stringIterator.hasNext())
              {
                  System.out.println(stringIterator.next());
              }
      

      运行流畅。

      您可以在此处了解有关迭代器的更多信息:https://www.geeksforgeeks.org/how-to-use-iterator-in-java/

      另外,我认为由于您在第二次调用时没有检查 hasNext() 两次调用迭代器,它可能会抛出 java.util.NoSuchElementException

      【讨论】:

      • 乐于助人:)
      • 对于您发布的代码中内部for 循环的每次迭代,您检索peopleIterator 的下一个值而不检查是否有下一个值。这是故意的吗?您可能会收到NoSuchElementException,不是吗?
      【解决方案3】:

      每次调用value.iterator() 时,都会从头开始创建新的Iterator 对象,指向第一个元素。为了避免它,将调用它的第一个结果存储到局部变量中。

      Iterator<Peple> it = value.iterator();
      
      while(it.hasNext()){
        // your remaiing code
      }
      

      【讨论】:

        【解决方案4】:

        试试这个:

        public Map<String, String> getKeysByValue(Map<String, Map<String, Peple>> map, Collection<Peple> value) {
        
            Map<String, String> stringStringMap = new HashMap<>();
            int count = 0;
            Iterator<Peple> iterator = value.iterator();
            while (iterator.hasNext()) {
                Peple p = iterator.next();
                for (Map.Entry<String, Map<String, Peple>> entry : map.entrySet()) {
                    for (Map.Entry<String, Peple> entry1 : entry.getValue().entrySet()) {
                        String verified = p.gerVerfied();
                        if (verified.equals("true")) { 
                            stringStringMap.put(entry1.getKey(), p.getName());
                        }
                    }
                }
            }
        }
        

        【讨论】:

          【解决方案5】:

          您在该迭代器上调用了 next() 两次。它将向前移动并获取下一个人的名字,这可能无法验证。

          如果它是经过验证的 Peple,我相信你只是想知道这个名字?

          while (value.iterator().hasNext()) {
            for (Map.Entry<String, Map<String, Peple>> entry : map.entrySet()) {
              for (Map.Entry<String, Peple> entry1 : entry.getValue().entrySet()) {
                Peple nextPeple = value.iterator().next();
                String verified = nextPeple.gerVerfied();
                if (verified.equals("true")) { 
                  stringStringMap.put(entry1.getKey(), nextPeple.getName());
                }
              }
            }
          }
          

          【讨论】: