简体   繁体   中英

Most efficient way to compare 2 different object attributes in a list

I have 2 ArrayLists of custom objects. I am making a nested loops to compare them and find matches.

However, is there anything better? I read something about retainAll for primitive types, but I can't find a way how to apply it here. I also read something about overriding the equals() , can't get that either, as this is two different objects.

for (String email : emailsOfContactsWhoFitDynConFilter) {
            for (Contact contact : emailClicks.items) { 
                if (email.equals(contact.EmailAddress) && (contact.link).split("\\?")[0].equals(linkInDynamicContent.split("\\?")[0])) {
                    count++;
                    break;
                }
        }

I wouldn't pretend this is the most efficient way , but I'd do something like that:

Set<String> emails = new HashSet<>(emailsOfContactsWhoFitDynConFilter);
String token = linkInDynamicContent.split("\\?")[0];
int count = items.stream().parallel()
        .filter(contact -> emails.contains(contact.EmailAddress))
        .filter(contact -> contact.link.split("\\?")[0].equals(token))
        .count();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM