Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

java - Collect a list of ids based on multiple fields

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

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

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]);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can watch here for a very good guide

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

Main-Class

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

}

TempObject

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

Person

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...