我如何将其转换为 java 流

How can i convert it to java stream

我是 java8 streams 的新手。我正在尝试使用 stream 处理对象集合。但无法以精确的方式实现。 下面是我实现的代码片段(给出了错误的结果)。预期的最终结果是“Names email@test.com”的 List<String>。 recordObjects 是对象的集合

choices = recordObjects.stream()
                    .filter(record -> record.getAttribute
                            (OneRecord.AT_RECORD_SUBMITTER_TABLE_EMAIL) != null)
                    .filter(record -> !record.getAttributeAsString
                            (OneRecord.AT_RECORD_SUBMITTER_TABLE_EMAIL).isEmpty())
                    .map(record -> record.getMultiValuedAttribute
                            (OneRecord.AT_RECORD_SUBMITTER_TABLE_EMAIL, String.class))
                    .flatMap(Collection::stream)
                    .map(email -> getFormattedEmailAddress(ATTRI_AND_RECORD_CONTACT_DEFAULT_NAME, email))
                    .collect(Collectors.toList());

但下面是我想使用流实现的确切逻辑。

for (CallerObject record : recordObjects) {
                List<String> emails = record.getMultiValuedAttribute(
                    OneRecord.AT_RECORD_SUBMITTER_TABLE_EMAIL, String.class);
                List<String> names = record.getMultiValuedAttribute(
                    OneRecord.AT_RECORD_SUBMITTER_TABLE_NAME, String.class);
                int N = emails.size();
                for (int i = 0 ; i < N ; i++) {
                    if(!isNullOrEmpty(emails.get(i)))
                    {
                        choices.add(getFormattedEmailAddress(isNullOrEmpty(names.get(i)) ?
                                ATTRI_AND_RECORD_CONTACT_DEFAULT_NAME : names.get(i) , emails.get(i)));
                    }
                }
            }

由于我们不知道 getFormattedEmailAddress 方法,所以我使用 String.format 来实现所需的表示 "Names email@test.com":

// the mapper function: using String.format
Function<RecordObject, String> toEmailString =  r -> {
  String email = record.getMultiValuedAttribute(OneRecord.AT_RECORD_SUBMITTER_TABLE_EMAIL, String.class);
  String name = record.getMultiValuedAttribute(OneRecord.AT_RECORD_SUBMITTER_TABLE_NAME, String.class);
  if (email != null) {
    return String.format("%s %s", name, email);
  } else {
    return null;
  }
};

choices = recordObjects.stream()
  .map(toEmailString)  // map to email-format or null
  .filter(Objects::nonNull)  // exclude null strings where no email was found
  .collect(Collectors.toList());

将您的旧版本代码更改为 Java 8

        final Function<RecordedObject, List<String>> filteredEmail = ro -> {
            final List<String> emails = ro.getMultiValuedAttribute(
                OneRecord.AT_RECORD_SUBMITTER_TABLE_EMAIL, String.class);
            final List<String> names = ro.getMultiValuedAttribute(
                OneRecord.AT_RECORD_SUBMITTER_TABLE_NAME, String.class);


            return IntStream.range(0, emails.size())
                .filter(index -> !isNullOrEmpty(emails.get(index)))
                .map(index -> getFormattedEmailAddress(isNullOrEmpty(names.get(index)) ?
                    ATTRI_AND_RECORD_CONTACT_DEFAULT_NAME : names.get(index) , emails.get(index)))
                .collect(Collectors.toList());
        };

        recordObjects
            .stream()
            .map(filteredEmail)
            .flatMap(Collection::stream)
            .collect(Collectors.toList());