【问题标题】:Filtering keys from Map to List attribute in Java 8在 Java 8 中过滤从 Map 到 List 属性的键
【发布时间】:2019-02-18 07:11:42
【问题描述】:

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

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

我是这样比较的:如果地图中的 id 与列表中的 id 匹配,则添加 从 Map 到 List 的年龄。

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

newmap.entrySet().stream().filter(entry->entry.getKey().equals(student::getId)).collect(..collect here to list..);

【问题讨论】:

  • 你从哪里得到student::getId?您可能需要两级映射/过滤..
  • 可以添加地图声明吗?
  • 你应该遵循 Java 命名约定:类名总是用 PascalCase 写的,所以它应该以大写开头。 IE。 student 应该是 Student

标签: java java-8 java-stream


【解决方案1】:
stulist = stulist.stream().map(instance -> {
    student studentInstance = instance; 
    studentInstance.setAge(newMap.getOrDefault(studentInstance.getId(),"<default age>"));
    return studentInstance;
}).collect(Collectors.toList()); ;

ps:使用正确的命名约定。将班级名称 student 更改为 Student。

【讨论】:

  • 他要求 java 8 解决方案,我猜他的意思是流解决方案
  • 尝试在java 8中给出sol
  • 已将其更改为 java 8。
【解决方案2】:

这里是解决方案,假设我的问题是正确的:

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

【讨论】:

    【解决方案3】:

    将 Student 类实现为 @Zgurskyi 注释,然后在 main 上使用它:

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

    【讨论】:

      【解决方案4】:

      使用Collectors.toList()

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

      【讨论】:

      • 我认为你的意思是 List&lt;String&gt; list = stulist.stream().map(student::getName).collect(Collectors.toList()); System.out.println(list); 但它返回 null,null,null
      • 我开始写同样的东西……你写得比我快——点赞:)
      猜你喜欢
      • 2021-09-03
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      • 2018-05-10
      • 1970-01-01
      • 2013-12-20
      • 2017-01-28
      相关资源
      最近更新 更多