javalistgroupingatomicinteger

Grouping based on preferences keep returning null value


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]


Solution

  • 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