Class PopulationIterator<T extends Comparable<T>>

java.lang.Object
net.bmahe.genetics4j.core.PopulationIterator<T>
Type Parameters:
T - the type of fitness values in the population, must be comparable for selection operations
All Implemented Interfaces:
Iterator<Individual<T>>

public class PopulationIterator<T extends Comparable<T>> extends Object implements Iterator<Individual<T>>
Iterator implementation for traversing individuals in a population during evolutionary algorithms.

PopulationIterator provides a standard Java Iterator interface for accessing individuals in a Population, combining genotypes with their corresponding fitness values to create complete Individual instances during iteration.

This iterator enables convenient traversal patterns such as:

  • Enhanced for loops: Iterate over individuals using for-each syntax
  • Stream operations: Convert populations to streams for functional processing
  • Sequential access: Process individuals one at a time without loading all into memory
  • Collection integration: Use with Java Collection framework methods

The iterator maintains internal state to track the current position and constructs Individual objects on-demand by combining genotypes and fitness values from the underlying population at the same index.

Key characteristics:

  • Type safety: Parameterized with fitness type for compile-time type checking
  • Lazy evaluation: Creates Individual objects only when requested
  • Memory efficient: Doesn't duplicate population data, references original
  • Standard interface: Implements Java Iterator contract completely

Usage patterns:


 // Enhanced for loop iteration
 Population<Double> population = getPopulation();
 for (Individual<Double> individual : population) {
     System.out.println("Fitness: " + individual.fitness());
 }
 
 // Stream-based processing
 population.stream()
     .filter(individual -> individual.fitness() > threshold)
     .mapToDouble(Individual::fitness)
     .average();
 
 // Manual iteration
 Iterator<Individual<Double>> iterator = population.iterator();
 while (iterator.hasNext()) {
     Individual<Double> individual = iterator.next();
     processIndividual(individual);
 }
 

Thread safety considerations:

  • Single-threaded use: Iterator instances are not thread-safe
  • Population stability: Underlying population should not be modified during iteration
  • Concurrent iterations: Multiple iterators can be created for the same population
See Also:
  • Field Details

    • population

      private final Population<T extends Comparable<T>> population
    • currentIndex

      private int currentIndex
  • Constructor Details

    • PopulationIterator

      public PopulationIterator(Population<T> _population)
      Constructs a new iterator for the specified population.

      Creates an iterator that will traverse all individuals in the given population, starting from index 0 and proceeding sequentially through all individuals.

      Parameters:
      _population - the population to iterate over
      Throws:
      IllegalArgumentException - if the population is null
  • Method Details

    • hasNext

      public boolean hasNext()
      Returns true if there are more individuals to iterate over.

      Checks whether the current position is within the bounds of the population. This method can be called multiple times without advancing the iterator position.

      Specified by:
      hasNext in interface Iterator<T extends Comparable<T>>
      Returns:
      true if there are more individuals, false if all have been visited
    • next

      public Individual<T> next()
      Returns the next individual in the population and advances the iterator position.

      Constructs an Individual by combining the genotype and fitness value at the current position, then advances to the next position for subsequent calls.

      Specified by:
      next in interface Iterator<T extends Comparable<T>>
      Returns:
      the next individual in the iteration sequence
      Throws:
      NoSuchElementException - if there are no more individuals (when hasNext() returns false)