收集基于多个字段的id列表

2022-01-22 00:00:00 list grouping collections java java-stream

我有一个包含 personId、年龄和性别的人员对象.

I have a person object with personId, age and gender.

public class Person {
    private int personId;
    private int age;
    private int gender; // 0 for male and 1 for female
}

List<Person> person = new Arraylist<>();
person.add(new Person(1,1,1));
person.add(new Person(2,2,0));
person.add(new Person(3,10,1));
person.add(new Person(4,11,0));
person.add(new Person(5,20,1));
person.add(new Person(6,20,1));
person.add(new Person(7,2,0));
person.add(new Person(8,20,0));
person.add(new Person(9,11,0));
person.add(new Person(10,20,1));

我想创建一个这样的临时对象,其中包含年龄、性别和学生 ID 列表.

I would like to create a temp object like this with age, gender and list of studentIds.

TempObject {
    private int age;
    private int gender;
    private List<Integer> studentIds;
}

现在,我想创建带有年龄、性别和学生 ID 列表的 TempObject.这个对象应该有一对年龄、性别和与年龄和性别对应的学生ID列表.有人可以帮我吗.我尝试过使用 java8 的分组方式.

Now, I want to create TempObject with with age, gender and list of studentIds. This object should have a pair of age, gender and list of student ids corresponding to age and gender. Can someone help me out. I have tried using java8's grouping by.

new TempObject(1,1,[1]);
new TempObject(2,0,[2,7]);
new TempObject(10,1,[3]);
new TempObject(11,0,[4,9]);
new TempObject(20,1,[5,6,10]);
new TempObject(20,0,[8]);

推荐答案

你可以在这里观看一个非常好的指南

you can watch here for a very good guide

无论如何,我希望它对你有帮助(也许有点).

Anyway, i hope it helps you (maybe a little bit).

主类

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class mainMethod {

    public static void main(String[] args) {
        List<Person> persons = new ArrayList<>();
        persons.add(new Person(1, 1, 1));
        persons.add(new Person(2, 2, 0));
        persons.add(new Person(3, 1, 1));
        persons.add(new Person(4, 11, 0));
        persons.add(new Person(5, 20, 1));
        persons.add(new Person(6, 20, 1));
        persons.add(new Person(7, 2, 0));
        persons.add(new Person(8, 20, 0));
        persons.add(new Person(9, 11, 0));
        persons.add(new Person(10, 20, 1));

        TempObjectMapper tempObjectMapper = new TempObjectMapper(persons.stream()
                .collect(Collectors.groupingBy(Person::getAge, Collectors.groupingBy(Person::getGender))));

        List<TempObject> tempObjects = tempObjectMapper.getObjects();

        System.out.println(tempObjects.toString());
    }
}

TempObjectMapper

TempObjectMapper

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TempObjectMapper {

    private Map<Integer, Map<Integer, List<Person>>> map;

    public TempObjectMapper(Map<Integer, Map<Integer, List<Person>>> collect) {
        this.map = collect;
    }

    public List<TempObject> getObjects() {
        List<TempObject> list = new ArrayList<TempObject>();

        this.map.forEach((key, value) -> {
            int age = key;
            Map<Integer,List<Person>> map1 = value;

            map1.forEach((key1, value1) -> {
                int gender = key1;
                List<Person> person = value1;
                list.add(new TempObject(age, gender, person));
            });
        });
        return list;
    }

}

临时对象

import java.util.List;

public class TempObject {
 
    private int age;

    private int gender;

    private List<Person> persons;

    public TempObject(int age, int gender, List<Person> persons) {
        this.age = age;
        this.gender = gender;
        this.persons = persons;
    }

    @Override
    public String toString() {
        return String.format("TempObject: [%s,%s,%s]" , this.age, this.gender, this.persons.toString());
    }
}

public class Person {
    private int personId;
    private int age;
    private int gender;  //0 for male and 1 for female

    public Person(int id, int age, int gender) {
        this.personId = id;
        this.age = age;
        this.gender = gender;
    }
    public int getPersonId() {
        return this.personId;
    }

    public int getAge() {
        return this.age;
    }

    public int getGender() {
        return this.gender;
    }

    @Override
    public String toString() {
        return String.format("Person: [%s, %s, %s]", this.personId,this.age,this.gender);
    }
 }

您可以使用此行过滤您的列表

You can filter your list with this line

persons.stream()              
.collect(Collectors.groupingBy(Person::getAge, Collectors.groupingBy(Person::getGender)))

相关文章