Filtering keys from Map to List attribute in Java 8

自作多情 提交于 2019-12-05 22:20:43

Here is solution, assuming I got the question right:

import java.util.*;
import java.util.stream.*;

public class Answer {
  public static void main(String[] args) {
    List<Student> studentsWithoutAge = Arrays.asList(
      new Student("1", "Vishwa", null),
      new Student("3", "Ravi", null),
      new Student("2", "Ram", null)
    );

    Map<String,String> ageById = new HashMap() {{
      put("1","20");
      put("2","30");
    }};

    List<Student> studentsWithAge = addAge(studentsWithoutAge, ageById);

    System.out.println("Students without age: " + studentsWithoutAge);
    System.out.println("Students with age: " + studentsWithAge);
  }

  static List<Student> addAge(List<Student> students, Map<String,String> ageById) {
    return students.stream()
                   .map(student -> {
                      String age = ageById.getOrDefault(student.getId(), null);
                      return new Student(student.getId(), student.getName(), age);
                   })
                   .collect(Collectors.toList());
  }
}

class Student {
  private String name;
  private String age;
  private String id;
  Student(String id,String name,String age){
    this.id = id;
    this.name = name;
    this.age = age;
  }
  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  public String getAge() {
      return age;
  }
  public void setAge(String age) {
      this.age = age;
  }
  public String getId() {
      return id;
  }
  public void setId(String id) {
      this.id = id;
  }
  @Override
  public String toString() {
    return String.format("Student: id = %s, name = %s, age = %s", this.id, this.name, this.age);
  }
}

stulist = stulist.stream().map(instance -> {
    student studentInstance = instance; 
    studentInstance.setAge(newMap.getOrDefault(studentInstance.getId(),"<default age>"));
    return studentInstance;
}).collect(Collectors.toList()); ;

ps: Use proper naming conventions. Change the class name student to Student.

Implement Student class as @Zgurskyi comment, and then use this on main:

stulist.forEach(stu -> {
        stu.setAge(newmap.getOrDefault(stu.getId(), null));
    });

Use Collectors.toList()

// Accumulate names into a List
List<String> list = people.stream().map(Person::getName).collect(Collectors.toList());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!