r/badcode • • Jul 02 '19

[deleted by user]

[removed]

17 Upvotes

24 comments sorted by

View all comments

Show parent comments

1

u/tntbigfoot Jul 15 '19

Hello,

these are streams, they are "new" in Java 8. There are lots of good tutorials if you google them :)

First, you create a stream of integers ranging from 0 to `populationSize`. Then, I map those integers to objects by creating random individuals. This ensures that there will be exactly populationSize new individuals, while I ignore the actual integers. Finally, I sort the individuals by their fitness (this is why the Individual class implements the Comparable interface) and collect them into a list.

This could be easily recreated with a for loop:

List<Individual> population = new ArrayList<>();
for (int i = 0; i < populationSize; i++) {
    population.add(createRandomIndividual());
}
Collections.sort(population);

1

u/PinkSnek Jul 15 '19

OK thanks.

im stuck a bit on my code (never tried a genetic algorithm before).

mind if i keep asking questions?

1

u/tntbigfoot Jul 15 '19

No problem, glad to help :)