title: Random generation of teams according to age and gender in Java
body: <p>I'm trying to develop a short program with which you can select random groups from a list of people (read from a csv file), which are nevertheless so fairly distributed that each group contains people of different ages and genders. The user can specify how many groups / teams there should be in total. In the meantime I have reached the point where I can load a csv file, select it and create an array list that contains an object of the &quot;Person&quot; class for each person. This is how the person class is structured:</p>
  <pre><code>public class Person implements Comparable&lt;Person&gt; {
  
      private Integer birthYear;
      private String name;
      private String surname;
      private String gender;
  
      public Person(String birthYear, String name, String surname, String gender) {
          this.birthYear = Integer.parseInt(birthYear);
          this.name = name;
          this.surname = surname;
          this.gender = gender;
      }
  
      @Override
      public int compareTo(Persono) {
          return this.getBirthYear().compareTo(o.getBirthYear());
      }
  
      public String printInfo() {
          String info = &quot;Surname: &quot; + surname + &quot; Name: &quot; + name + &quot; Year of Birth: &quot; + birthYear + &quot; Gender: &quot; + gender;
          System.out.println(info);
          return info;
      }
  
      public Integer getBirthYear() {
          return birthYear;
      }
  
      public String getName() {
          return name;
      }
  
      public String getSurname() {
          return surname;
      }
  
      public String getGender() {
          return gender;
      }
  
      public void setBirthYear(Integer birthYear) {
          this.birthYear = birthYear;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public void setSurname(String surname) {
          this.surname = surname;
      }
  
      public void setGender(String gender) {
          this.gender= gender;
      }
  
  }
  </code></pre>
  <p>I'm trying to create a class &quot;Manager&quot; that already splits an array list with people into two array lists according to gender.
  Another method in the manager class uses an array list with people to create a map that has an integer year of birth as a key and an array list with the people who have the value of the integer as a year of birth as the value.</p>
  <pre><code>public class Manager {
  
      ArrayList&lt;Person&gt; male = new ArrayList&lt;&gt;();
      ArrayList&lt;Person&gt; female = new ArrayList&lt;&gt;();
  
      public Manager(ArrayList&lt;Person&gt; people, int numOfTeams) {
          splitSex(people);
          sortByAge();
          generateGroups(male, female, numOfTeams);
      }
  
      private void splitSex(ArrayList&lt;Person&gt; people) {
          if (people != null) {
              for (Person person : Person) {
                      if (person.getGender().equals(&quot;m&quot;)) {
                          male.add(person);
                      } else if (person.getGender().equals(&quot;f&quot;)) {
                          female.add(person);
                      } else
                          System.err.println(person.getGender() + &quot;is not a Gender.&quot;);
                  }
              }
          }
  
      }
  
      private void sortByAge() {
          Collections.sort(male);
          Collections.sort(female);
      }
  
      private void generateGroups(ArrayList&lt;Person&gt; male, ArrayList&lt;Person&gt; female, int numOfGroups) {
          males = getAgeSortedGroups(male);
          females = getAgeSortedGroups(female);
      }
  
      private Map&lt;Iteger, List&lt;Person&gt; getAgeSortedGroups(ArrayList&lt;Person&gt; list) {
          Map&lt;Integer, List&lt;Person&gt;&gt; ageGroups = list.stream().collect(Collectors.groupingBy(Person::getBirthYear));
          return ageGroups;
      }
  
  }
  </code></pre>
  <p>(I think the method &quot;sortByAge()&quot; and the implementation of &quot;Compareble&quot; aren't necessary anymore bcause of the method &quot;getAgeSortedGroups(ArrayList list)&quot;).</p>
  <p>Now i want to somehow write a method that generates similar, fair, but still random groups / teams from the two maps (one map with male people, the other with female people).</p>
  <p>E.g.: The user says he wants to generate 10 groups/teams and the Csv-File includes 60 People of males and females and all born between 2002 and 2010. Now I want that each of the 10 teams includes the same proportion of males and females and of the different ages.</p>
  <p>I guess I have to somehow write a loop in which the groups (as lists) are created and the people are assigned. but i have no clue how.
  I hope you get my problem (I am no native speaker).</p>
  <p>Thanks for your help!</p>