Functions at the input is a class whose 3 fields are HashMap and 3 possible values ​​of these fields (3 possible values). Next I need to determine if one of those 3 values ​​lies in each map. And if it occurs in some kind of map, then I increase the internal counter. I run like this: I am passing the iterator and if there is a coincidence iterator.next () with the possible field value, I increment the counter.

In the process there was such an error:

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

What does it mean?

 Iterator<Segment.Age> ageIterator = campaign.getTarget().getAge().iterator(); while ( ageIterator.hasNext() ){ if ( mapFromAgeToAgeDistribution.containsKey(ageIterator.next()) ){ ageFitness += mapFromAgeToAgeDistribution.get(ageIterator.next()); // рагается на эту строчку } } 
  • It is easier to do: try to add to each set this value, which will not be added - to increase the counter or check the container. - Gorets
  • But how to determine whether it was added or not? - Stas0n
  • add method - returns false - Gorets 6:02
  • oh, blame, stupid ... what to do if the possible values ​​lie in the HashSet? those. I need to check whether all the elements of the set are among the elements of this HashSet. And if some are lying, then you need to get the values ​​of the matched keys. - Stas0n

1 answer 1

Swears, because you perform ageIterator.next () twice in one run of the loop. Accordingly, in the if condition, you have one value checked, and in the if body, another is used. Do it in this way:

 Iterator<Segment.Age> ageIterator = campaign.getTarget().getAge().iterator(); while ( ageIterator.hasNext() ){ Segment.Age ageItem = ageIterator.next(); if ( mapFromAgeToAgeDistribution.containsKey(ageItem) ){ ageFitness += mapFromAgeToAgeDistribution.get(ageItem); } } 

And there will be happiness.