Я хочу, чтобы вы перебирали schoolData, получали каждого ученика и устанавливали вес для каждого соответствующего ученика из weightData - PullRequest
0 голосов
/ 17 июня 2020
public class Main {
    public static void main(String[] args) {

        // Do not modify from here --->>
        Map<String, List<Student>> schoolData = new HashMap<>();

        // class 1 data
        List<Student> studentList = new ArrayList<>();

        Student student = new Student();
        student.setName("ram");
        student.setAge(12);

        studentList.add(student);

        student = new Student();
        student.setName("bheem");
        student.setAge(11);

        studentList.add(student);

        schoolData.putIfAbsent("class1", studentList);

        System.out.println("Class1" + studentList);
        // class 2 data
        List<Student> studentList2 = new ArrayList<>();

        Student student2 = new Student();
        student2.setName("jessica");
        student2.setAge(12);

        studentList2.add(student2);

        student2 = new Student();
        student2.setName("katrina");
        student2.setAge(11);

        studentList2.add(student2);

        schoolData.putIfAbsent("class2", studentList2);
        System.out.println("Class2" + studentList2);

        Map<String, Integer> studentWeightData = new HashMap<>();
        studentWeightData.putIfAbsent("ram", 32);
        studentWeightData.putIfAbsent("bheem", 48);
        studentWeightData.putIfAbsent("jessica", 26);
        studentWeightData.putIfAbsent("katrina", 24);

Я создал класс ученика

Используя java Пожалуйста, помогите мне выполнить итерацию schoolData и установить вес для каждого соответствующего ученика из weightData.

1 Ответ

0 голосов
/ 17 июня 2020
        //you have data for {student:weight} as source of truth
        //The data to manipulate is schoolData which is {class:Students}
        //we have to iterate the studentWeightData if the weight is present for student then we have to update it
        for(Map.Entry<String, List<Student>> entries : schoolData.entrySet()){
            List<Student> students = entries.getValue();
            for (Student studentTemp : students) {
                String studentName = studentTemp.getName();
                Integer studentWeight = studentWeightData.get(studentName);
                if (Objects.nonNull(studentWeight)) {
                    studentTemp.setWeight(studentWeight);
                }
            }
        }

Полный код:


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

public class Test {
    public static void main(String[] args) {
        // Do not modify from here --->>
        Map<String, List<Student>> schoolData = new HashMap<>();

        // class 1 data
        List<Student> studentList = new ArrayList<>();

        Student student = new Student();
        student.setName("ram");
        student.setAge(12);

        studentList.add(student);

        student = new Student();
        student.setName("bheem");
        student.setAge(11);

        studentList.add(student);

        schoolData.putIfAbsent("class1", studentList);

        System.out.println("Class1" + studentList);
        // class 2 data
        List<Student> studentList2 = new ArrayList<>();

        Student student2 = new Student();
        student2.setName("jessica");
        student2.setAge(12);

        studentList2.add(student2);

        student2 = new Student();
        student2.setName("katrina");
        student2.setAge(11);

        studentList2.add(student2);

        schoolData.putIfAbsent("class2", studentList2);
        System.out.println("Class2" + studentList2);

        Map<String, Integer> studentWeightData = new HashMap<>();
        studentWeightData.putIfAbsent("ram", 32);
        studentWeightData.putIfAbsent("bheem", 48);
        studentWeightData.putIfAbsent("jessica", 26);
        studentWeightData.putIfAbsent("katrina", 24);

        //you have data for student:weight as source of truth
        //The data to manipulate is schoolData which is class:Students
        for (Map.Entry<String, List<Student>> entries : schoolData.entrySet()) {
            List<Student> students = entries.getValue();
            for (Student studentTemp : students) {
                String studentName = studentTemp.getName();
                Integer studentWeight = studentWeightData.get(studentName);
                if (Objects.nonNull(studentWeight)) {
                    studentTemp.setWeight(studentWeight);
                }
            }
        }
    }
}

class Student {
    private String name;
    private Integer weight;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...