Trying to speed up the process of validation the items using stream api. Here is my code:
List<VodEpisodesDatum> allEpisodes = vodEpisodesSteps.getAllV1VodEpisodesFullSync(syncStop, null);
List<VodEpisodesDatum> movieEpisodes = vodEpisodesSteps.getAllV1VodEpisodesFullSync(syncStop, props);
List<VodEpisodesDatum> notMovieEpisodesTemp = new ArrayList<>();
List<VodEpisodesDatum> movieEpisodesTemp = new ArrayList<>();
List<VodEpisodesDatum> allHandledEpisodes = new ArrayList<>();
int amountToValidate = 100;
Collections.nCopies(amountToValidate, 1).parallelStream()
.map(i -> RandomUtil.randomInteger.apply(allEpisodes.size()))
.forEach(rand -> {
if(isNotMovie.test(allEpisodes.get(rand))) notMovieEpisodesTemp.add(allEpisodes.get(rand));
if(isMovie.test(allEpisodes.get(rand))) movieEpisodesTemp.add(allEpisodes.get(rand));
allHandledEpisodes.add(allEpisodes.get(rand));
});
public class RandomUtil {
public static IntFunction<Integer> randomInteger = r -> (int) ((Math.random() * (r - 1)));
}
But from time to time when I run this block of code i'm getting allHandledEpisodes != amountToValidate
. How can it be??? It looks like a some kind of magic. I suspect that .map() does not work as expected here, but I don't really understand what's going on.