Reputation: 394
I am designing a group generator that takes in preferences such as “mix gender”, “mix nationality”... I am putting a list of student names, followed by nationality and gene set, in an arraylist. What is the easiest way to generate groups, based on user input, that each group consists of people from different nationalities, or balanced gender.
public ArrayList<String> readEachWord(String className)
{
ArrayList<String> readword = new ArrayList<String>();
Scanner sc2 = null;
try {
sc2 = new Scanner(new File(className + ".txt"));
} catch (FileNotFoundException e) {
System.out.println("error, didnt find file");
e.printStackTrace();
}
while (sc2.hasNextLine()) {
Scanner s2 = new Scanner(sc2.nextLine());
while (s2.hasNext()) {
String s = s2.next();
readword.add(s);
}
}
return readword;
}
I am using this to read a text file, and on each line, I have each student's name nationality and gender. I put them into an ArrayList and am right now trying to figure out how to evenly distribute them based on the user-desired group numbers.
I am using a txt file to store all the information since this group generator is customized for my school.
Upvotes: 0
Views: 355
Reputation: 16526
Here's something to get you started. This short algo will sort people based on the specified discriminator (nationality, gender etc.) and then distribute them evenly into the specified amount of groups.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Main {
private static class Person {
private String nationality;
private String gender;
public Person(String nationality, String gender) {
this.nationality = nationality;
this.gender = gender;
}
public String getNationality() {
return nationality;
}
public String getGender() {
return gender;
}
@Override
public String toString() {
return "Person{" +
"nationality='" + nationality + "'" +
", gender='" + gender + "'" +
"}";
}
}
public static void main(String[] args) {
List<Person> people = Arrays.asList(
new Person("DE", "male"),
new Person("DE", "female"),
new Person("DE", "n/a"),
new Person("UK", "female"),
new Person("UK", "female"),
new Person("UK", "female"),
new Person("JP", "trans"),
new Person("JP", "male")
);
int number_of_groups = 3;
Function<Person, String> discriminator = Person::getGender;
AtomicInteger index = new AtomicInteger();
List<List<Person>> groups = new ArrayList<>(people.stream()
.sorted(Comparator.comparing(discriminator))
.collect(Collectors.groupingBy(e -> index.getAndIncrement() % number_of_groups))
.values());
groups.forEach(System.out::println);
}
}
Output:
[Person{nationality='DE', gender='female'}, Person{nationality='UK', gender='female'}, Person{nationality='JP', gender='trans'}]
[Person{nationality='UK', gender='female'}, Person{nationality='DE', gender='male'}]
[Person{nationality='UK', gender='female'}, Person{nationality='DE', gender='n/a'}]
Upvotes: 1