在Java 8中从“映射到列表”属性过滤键

时间:2019-02-18 07:11:43

标签: java java-8 java-stream

我有一个列表和一个地图,如下所示:

channel.finish()
  

我正在像这样进行比较:如果地图中的ID与列表中的ID相匹配,则添加   从地图的年龄到列表的年龄。

到目前为止,我已经尝试过了,但是我无法得到它。

public class student {
private String name;
private String age;
private String id;
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;
}
student(String id,String name,String age)
{
    }
}
List<student> stulist = Arrays.asList(new student("1", "vishwa",null),
                                              new student("3", "Ravi",null),
                                              new student("2", "Ram",null));

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

4 个答案:

答案 0 :(得分:2)

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

ps:使用正确的命名约定。将班级名称为“学生”。

答案 1 :(得分:1)

这是解决方案,假设我正确地回答了这个问题:

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

答案 2 :(得分:1)

将学生类作为@Zgurskyi注释实施,然后在main上使用它:

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

答案 3 :(得分:0)

使用Collectors.toList()

// Accumulate names into a List
List<String> list = people.stream().map(Person::getName).collect(Collectors.toList());