Why does not it work? The error constantly gets out:

at java.util.HashMap$HashIterator.nextEntry(Unknown Source) at java.util.HashMap$KeyIterator.next(Unknown Source) 

Here is the code itself:

 HashMap<Age, Integer> mapFromAgeToValues = new HashMap<Age, Integer>(); mapFromAgeToValues.put(Age.AGE1, 1); mapFromAgeToValues.put(Age.AGE2, 11); mapFromAgeToValues.put(Age.AGE3, 111); mapFromAgeToValues.put(Age.AGE4, 1333); HashSet<Age> example = new HashSet<Age>(); example.add(Age.AGE2); Integer r = 0; Iterator it = example.iterator(); while ( it.hasNext() ){ if ( mapFromAgeToValues.containsKey(it.next()) ){ r += mapFromAgeToValues.get(it.next()); } } out.println(r); 

Age - It's just enum

    1 answer 1

    Because it's wrong to do this

      if ( mapFromAgeToValues.containsKey(it.next()) ){ r += mapFromAgeToValues.get(it.next()); } 

    the first it.next () you already go to the next element try something like this

     Age currentAge = it.next(); if ( mapFromAgeToValues.containsKey(currentAge) ){ r += mapFromAgeToValues.get(currentAge); } 
    • Thanks, buddy! A night in the yard, the brain no longer thinks) + you in karma) - Stas0n