简体   繁体   English

比较列表中2个不同对象属性的最有效方法

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

I have 2 ArrayLists of custom objects. 我有2个自定义对象的ArrayLists 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. 我读了一些有关用于原始类型的retainAll的内容,但是在这里找不到如何应用它的方法。 I also read something about overriding the equals() , can't get that either, as this is two different objects. 我还阅读了有关重写equals() ,因为这是两个不同的对象,所以也无法做到这一点。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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