在一个流中转换两个流并Map()

epfja78i  于 2021-07-11  发布在  Java
关注(0)|答案(2)|浏览(294)

我有一个studentlogin数组,其中name是键,value是value

"studentLogin" : [ 
        {
            "name" : "firstName_key",
            "value" : "<actual first Name value>"
        },
        {
            "name" : "lastName_key",
            "value" : "<actual last Name value>"
        },
        ....
      ]

我有一个方法,我把studentlogin列表作为一个输入参数,我需要检查firstname(key)和lastname(key)是否在同一个索引中,如果是,那么我需要从同一个索引中连接firstname和lastname的实际值。
我写了下面的方法,但使用两个流,我想把它转换成一个流。

public String convertStudentLoginToFullName(List<StudentLogin> studentLogin) {
      if (null != studentLogin) {
        String firstName = studentLogin.stream()
                .filter(x -> "firstName_key".equalsIgnoreCase(x.getName()))
               .map(x->x.getValue())
                .findFirst()
                .orElse(null);
        String lastName = studentLogin.stream()
                .filter(x -> "lastName_key".equalsIgnoreCase(x.getName()))
                .map(x -> x.getValue())
                .findFirst()
                .orElse(null);
        String fullName=firstName+" "+lastName;
        return fullName;
      }
    }
    return null;
  }
neekobn8

neekobn81#

一种方法是转换你的 StudentLoginMap 第一:

Map<String, String> studentLoginMap = studentLogin.stream()
    .collect(Collectors.toMap(
        StudentLogin::getName(),
        StudentLogin::getValue(),
        (old, new) -> old
    ))

注意如果有多个 StudentLogin 我使用了merge函数 (old, new) -> old 解决冲突。总是使用旧值来匹配您在原始代码中所做的操作-取第一个匹配的值 firstName_key 以及 lastName_key .
现在,您可以访问 studentLoginMap 如果名和姓都存在,则连接:

String firstName = studentLoginMap.get("firstName_key");
String lastName = studentLoginMap.get("lastName_key");
if (firstName != null && lastName != null) {
   String result = firstName + " " + lastName;
}
cmssoen2

cmssoen22#

在这种情况下,您可以使用 Collectors.toMap​(keyMapper,valueMapper) 具体如下:

public static void main(String[] args) {
    List<StudentLogin> studentLoginList = List.of(
            new StudentLogin(null, null),
            new StudentLogin("firstName_key", "<actual first Name value>"),
            new StudentLogin("lastName_key", "<actual last Name value>"));

    Map<String, String> studentLoginMap = studentLoginList.stream()
            // check if name and value are not null
            .filter(stlog -> stlog.getName() != null
                    && stlog.getValue() != null)
            // collect to map
            .collect(Collectors.toMap(
                    StudentLogin::getName,
                    StudentLogin::getValue));

    String firstName = studentLoginMap.get("firstName_key");
    String lastName = studentLoginMap.get("lastName_key");

    if (firstName != null && lastName != null) {
        System.out.println(firstName + " " + lastName);
        // <actual first Name value> <actual last Name value>
    }
}
public static class StudentLogin {
    String name;
    String value;

    public StudentLogin(String name, String value) {
        this.name = name;
        this.value = value;
    }

    public String getName() { return name; }
    public String getValue() { return value; }
}

另请参见:使用冗余值反转贴图以生成多重贴图

相关问题