简体   繁体   English

基于首选项的分组不断返回 null 值

[英]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这是我的人 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.我正在尝试按偏好对人们进行分组,我最初是在 greenfoot 上编写的,它运行良好。 However, I recently wanted to embed this in a project in intellij, with javafx.但是,我最近想用 javafx 将它嵌入到 intellij 的一个项目中。 When I was testing, it system output null.当我测试时,它系统 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]

Why are you sorting the values and using AtomicInteger?为什么要对值进行排序并使用 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注意:你的人 class 缺少一些东西,比如

  • A class declaration class 声明
  • Fields字段
  • and a toString() override (I had to make up one).和一个 toString() 覆盖(我必须弥补一个)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM