Java8 Stream loop iteration

Please help me do this in the Java8 streamAPI,

for(ContactDto contact : contactList){
    for(ContactContactTypeDto contactType : contact.getContactTypes()){
        if(PRIMARY_CONTACT.equals(contactType.getIdContactTypeCode())){
            StringBuilder contactNameSB = new StringBuilder(contact.getFirstName());
            contactNameSB.append(" ");
            if(null!=contact.getMiddleName() && !contact.getMiddleName().isEmpty()){
                contactNameSB.append(contact.getMiddleName());
                contactNameSB.append(" ");
            }
            contactNameSB.append(contact.getLastName());

            contactName = contactNameSB.toString();
            contactEmail = contact.getEmailAddress();
        }
    }
}

I tried, but I only reach

contactList.stream()
    .filter(contact -> contact.getContactTypes()
        .stream()
        .anyMatch(contactType -> PRIMARY_CONTACT.equals(contactType.getIdContactTypeCode())));
+3
source share
4 answers

When the code with java 8 threads is confused, it is useful to create some additional types and methods. For instance.

The method of creating the full name from its parts (you do not need it StringBuilder, in this case the compiler will use one):

String createFullName(ContactDto contact) {
    String contactName = contact.getFirstName() + " ";
    if (null != contact.getMiddleName() && !contact.getMiddleName().isEmpty()) {
        contactName += contact.getMiddleName() + " ";
    }
    return contactName + contact.getLastName();
}

A class for storing the result, basically a couple of names and email (add constructor, getters, etc.):

class Contact {
    private String name;
    private String email;
}

And now the code is much easier:

Optional<Contact> contact = contactList.stream()
        .filter(c -> c.getContactTypes()
                .stream()
                .map(ContactContactTypeDto::getIdContactTypeCode)
                .anyMatch(PRIMARY_CONTACT::equals))
        .findFirst()
        .map(c -> new Contact(createFullName(c), c.getEmailAddress()));

, , findFirst, Optional, , , .

map Optional<ContactDTO>, , Contact Optional<Contact>.

+3

, Stream ContactDto, :

ContactDto contact =
    contactList.stream()
               .filter(c -> c.getContactTypes()
                             .stream()
                             .anyMatch(t->PRIMARY_CONTACT.equals(t.getIdContactTypeCode())))
               .findFirst()
               .orElse(null);

, :

if (contact != null) {
    StringBuilder contactNameSB = new StringBuilder(contact.getFirstName());
    contactNameSB.append(" ");
    if(null!=contact.getMiddleName() && !contact.getMiddleName().isEmpty()) {
        contactNameSB.append(contact.getMiddleName());
        contactNameSB.append(" ");
    }
    contactName = contactNameSB.toString();
    contactEmail = contact.getEmailAddress();
}

:

ContactDto.

+2
List<ContactDto> contactListWithPrimaryContact = contactList.stream()
                .filter(contact -> contact.getContactTypeDto().parallelStream()
                        .anyMatch(x -> x.getIdContactTypeCode().equals(PRIMARY_CONTACT)))
                .collect(Collectors.toList());

primaryContact .

PRIMARY_CONTACT .

because as for the second contact in the list with the contact type as PRIMARY, in accordance with the solution above, we ignore it.

0
source
List<String> listOfPrimaryEmailAddresses = contactList.stream()
                                                      .filter(primaryContactPredicate())
                                                      .map(ContactDto::getEmailAddress)
                                                      .collect(Collectors.toList());


List<String> primaryContactNamesList = contactList.stream()
                                                  .filter(primaryContactPredicate())
                                                  .map(ContactDto::getFullName)
                                                  .collect(Collectors.toList());

private Predicate<ContactDto> primaryContactPredicate() {
        return contact -> contact.getContactTypes().stream().anyMatch(contactType -> PRIMARY_CONTACT.equals(contactType.getIdContactTypeCode()));
}

in the addDto method of the ContactDto class for getFullName ()

public String getFullName() {
        if (null != getMiddleName() && !getMiddleName().isEmpty()) {
            return getFirstName() + " " + getMiddleName() + " " + getLastName();
        }
        return getFirstName() + " " + getLastName();
    }
0
source

Source: https://habr.com/ru/post/1679157/


All Articles