-1
public class HomeroomMix {


    public void calculate(String className, int number) {
        List<PeopleClass> people = Arrays.asList(
                new PeopleClass("DE", "male"),
                new PeopleClass("DE", "female"),
                new PeopleClass("DE", "n/a"),
                new PeopleClass("UK", "female"),
                new PeopleClass("UK", "female"),
                new PeopleClass("UK", "female"),
                new PeopleClass("JP", "trans"),
                new PeopleClass("JP", "male")
        );


        int number_of_groups = number;

        Function<PeopleClass, String> discriminator = PeopleClass::getGender;

        AtomicInteger index = new AtomicInteger();
        List<List<PeopleClass>> groups = new ArrayList<>(people.stream()
                .sorted(Comparator.comparing(discriminator))
                .collect(Collectors.groupingBy(e -> index.getAndIncrement() % number_of_groups))
                .values());

        groups.forEach(System.out::println);
    }

this is my people class

 public PeopleClass(String nationality, String gender) {
        this.gender = gender;
        this.nationality = nationality;
    }

    public String getNationality() {
        return nationality;
    }

    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHomeRoom() {
        return homeRoom;
    }

    public void setHomeRoom(String homeRoom) {
        this.homeRoom = homeRoom;
    }

    @Override
    public String toString() {
        return name;
    }
}

I am trying to group people by preferences, and I originally programmed this on greenfoot and it worked perfectly. However, I recently wanted to embed this in a project in intellij, with javafx. When I was testing, it system output null. Though I am also planning to loop a list of objects instead of adding each in the list - i tried that and it kept sending nulls.

[null, null, null]
[null, null, null]
[null, null]

I want to print something like

[person1, person3, person 6]
[person 5, person 2, person 4]
[person 7, person 8]

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Steven Oh
  • 382
  • 3
  • 14
  • Can you also post your `PeopleClass` code? Specifically the `getGender` method. It would also help to see what you want the output to look like. – kingkupps May 02 '20 at 02:11
  • @kingkupps I added! – Steven Oh May 02 '20 at 02:20
  • Thanks! Can you also add what you **actually want** this to print? – kingkupps May 02 '20 at 02:21
  • @kingkupps added that too! – Steven Oh May 02 '20 at 02:28
  • Are the numbers in your desired output accurate? I don't see a pattern between the people in each list. They don't seem to be grouped by gender – kingkupps May 02 '20 at 02:35
  • @kingkupps hi, it’s supposed to mix the people so that each group comprises that owns a different attribute. For instance, a group with 3 people all from different nationality or mixed in gender. – Steven Oh May 02 '20 at 02:37
  • @StevenOh I've been looking at this and I don't exactly understand how the sorting criteria and the groupSize are supposed to work together. I could understand one or the other but not both. – WJS May 04 '20 at 14:54

1 Answers1

1

Why are you sorting the values and using AtomicInteger? Try it like this.

First, I added some names

int i = 1;
for (PeopleClass p : people) {
    p.setName("Person" + i++);
}

Then I grouped on gender


List<List<PeopleClass>> groups = new ArrayList<>(people
        .stream()
        .collect(Collectors
                .groupingBy(PeopleClass::getGender))
        .values());

And printed them.

groups.forEach(System.out::println);

Here's the result

[Person3:n/a]
[Person2:female, Person4:female, Person5:female, Person6:female]
[Person1:male, Person8:male]
[Person7:trans]

Note: Your People class is missing some things like

  • A class declaration
  • Fields
  • and a toString() override (I had to make up one).
WJS
  • 36,363
  • 4
  • 24
  • 39