-1

i have a array of StudentLogin where name is the key and value is value

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

i have a method which i get List of studentLogin as an input parameter,i need to check if firstName(key) and lastName(key) is present in the same index or not if yes then i need to concat the actual value of firstname and lastname from same index.

i wrote the below method but using two streams,i want to convert it into one stream.

  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;
  }
zamming
  • 21
  • 6
  • If I understand correctly, you want ` `? – Sweeper Nov 18 '20 at 06:46
  • yes,thats correct. – zamming Nov 18 '20 at 07:21
  • why is that `studentLogin ` even a list? don't you think, an object could have represented the information better? – Naman Nov 18 '20 at 10:32
  • yea good question,think in this way,lets say student is login in into different applications in the university so in that scenario the studentLogin may have different id/pwd for all of them so created a list, makes sense? – zamming Nov 19 '20 at 18:51

2 Answers2

1

One way is to convert your list of StudentLogin to a Map first:

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

Note that if there are multiple StudentLogins with the same name, I use the merge function (old, new) -> old to resolve the conflict. Always use the old value matches what you did in your original code - taking the first that matches firstName_key and lastName_key.

Now, you can access the values of studentLoginMap and concatenate if both first and last name exist:

String firstName = studentLoginMap.get("firstName_key");
String lastName = studentLoginMap.get("lastName_key");
if (firstName != null && lastName != null) {
   String result = firstName + " " + lastName;
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

In this case you can use method Collectors.toMap​(keyMapper,valueMapper) as follows:

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

See also: Invert a Map with redundant values to produce a multimap