基于多个字段收集ID列表

gcmastyq  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(518)

我有一个人的对象与人名,年龄和性别。

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));

我想创建一个临时对象这样的年龄,性别和学生名单。

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

现在,我想用年龄,性别和学生列表创建tempobject。这个对象应该有一对年龄、性别和对应于年龄和性别的学生ID列表。有人能帮帮我吗。我试过使用java8的分组方式。

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]);
nsc4cvqm

nsc4cvqm1#

你可以在这里看一本很好的指南
不管怎样,我希望它能帮助你(也许有一点)。
主要类别

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

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);
    }
 }

您可以使用此行筛选列表

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

ctzwtxfj2#

你可以用 Collectors.toMap(keyMapper,valueMapper,mergeFunction,mapFactory) 收集数据的方法 TreeMap 通过两个字段(年龄和性别)比较重复项的数量,并将ID合并到列表中:

List<Person> persons = Arrays.asList(
        new Person(1, 1, 1),
        new Person(2, 2, 0),
        new Person(3, 10, 1),
        new Person(4, 11, 0),
        new Person(5, 20, 1),
        new Person(6, 20, 1),
        new Person(7, 2, 0),
        new Person(8, 20, 0),
        new Person(9, 11, 0),
        new Person(10, 20, 1));
ArrayList<TempObject> tempObjects =
        new ArrayList<>(persons.stream()
                // convert Person to TempObject
                .map(e -> new TempObject(e.getAge(), e.getGender(),
                        Collections.singletonList(e.getPersonId())))
                // collect a Map<TempObject, TempObject>
                .collect(Collectors.toMap(
                        // key of the map
                        Function.identity(),
                        // value of the map
                        Function.identity(),
                        // merge function
                        (to1, to2) -> {
                            // merging two lists of ids
                            to1.setStudentIds(List
                                    .of(to1.getStudentIds(), to2.getStudentIds())
                                    .stream()
                                    .flatMap(List::stream)
                                    .distinct()
                                    .collect(Collectors.toList()));
                            return to1;
                        },
                        // map factory - specify a comparator
                        // for duplicates by age and gender
                        () -> new TreeMap<>(Comparator
                                .comparing(TempObject::getGender)
                                .thenComparing(TempObject::getAge))))
                // get a Collection
                // of the values
                .values());
tempObjects.forEach(System.out::println);
// age=2, gender=0, studentIds=[2, 7]
// age=11, gender=0, studentIds=[4, 9]
// age=20, gender=0, studentIds=[8]
// age=1, gender=1, studentIds=[1]
// age=10, gender=1, studentIds=[3]
// age=20, gender=1, studentIds=[5, 6, 10]

另请参见:基于值排序列表<map<string,object>>

相关问题