Find an Unique Email Address in a List – 查找列表中的唯一电子邮件地址

最后修改: 2024年 2月 25日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.导言

Email addresses make application software more efficient in user identification and communication. In the case of a list of email addresses, the distinction between unique addresses and the removal of duplicates becomes necessary.

电子邮件地址使应用软件在用户识别和通信方面更加高效。在电子邮件地址列表中,有必要区分唯一地址和删除重复地址。

In this tutorial, we’ll analyze multiple methods that can be applied to find unique email addresses in a list using Java.

在本教程中,我们将分析可用于使用 Java 在列表中查找唯一电子邮件地址的多种方法。

2. String Manipulation Approach

2.字符串操纵方法

For the list of email addresses, the goal is to pick up the unique email addresses, ignoring case sensitivity. For example, “User@example.com” and “user@example.com” are the same email addresses.

对于电子邮件地址列表,目标是拾取唯一的电子邮件地址,忽略大小写敏感性。例如,”User@example.com“和”user@example.com“是相同的电子邮件地址。

In this approach, we can use a HashSet to store unique emails efficiently, as the nature of a Hashset ensures that duplicate emails are automatically discarded. Let’s take an example:

在这种方法中,我们可以使用 HashSet 高效地存储唯一的电子邮件,因为 Hashset 的性质确保了重复的电子邮件会被自动丢弃。让我们举个例子:

@Test
public void givenEmailList_whenUsingStringManipulation_thenFindUniqueEmails() {
    Set<String> uniqueEmails = new HashSet<>();
    for (String email : emailList) {
        uniqueEmails.add(email.toLowerCase());
    }

    assertEquals(expectedUniqueEmails, uniqueEmails);
}

Here, we start by initializing a HashSet named uniqueEmails to store unique email addresses. Afterward, the loop iterates through each email in the provided list, converting it to lowercase using the toLowerCase() method to make the comparison case-insensitive. Then, we add this pre-processed email to the uniqueEmails HashSet.

在这里,我们首先初始化一个名为 uniqueEmailsHashSet 来存储唯一的电子邮件地址。然后,循环遍历所提供列表中的每封邮件,使用 toLowerCase() 方法将其转换为小写,使比较不区分大小写。然后,我们将预处理后的电子邮件添加到 uniqueEmails 哈希集中。

Finally, we employ the assertEquals() method to verify that the emails in expectedUniqueEmails match the email uniqueEmails set obtained through the string manipulation process.

最后,我们使用 assertEquals() 方法来验证 expectedUniqueEmails 中的电子邮件是否与通过字符串操作流程获得的电子邮件 uniqueEmails 集相匹配。

3. Java Streams Approach

3.Java 流方法

Java Streams provides an easy solution for processing collections by utilizing the filtering option and collecting unique email addresses. Let’s delve into a simple example:

JavaStreams通过使用过滤选项和收集唯一的电子邮件地址,为处理集合提供了一个简单的解决方案。让我们深入了解一个简单的示例:

@Test
public void givenEmailList_whenUsingJavaStreams_thenFindUniqueEmails() {
    Set<String> uniqueEmails = Arrays.stream(emailList)
      .map(String::toLowerCase)
      .collect(Collectors.toSet());

    assertEquals(expectedUniqueEmails, uniqueEmails);
}

In this test method, we first convert each email to lowercase using the toLowerCase() method. Also, we utilize the toSet() method to collect elements into a HashSet.

在此测试方法中,我们首先使用 toLowerCase() 方法将每封邮件转换为小写。此外,我们还使用 toSet() 方法将元素收集到 HashSet 中。

4. Conclusion

4.结论

In conclusion, we have presented various techniques for isolating exclusive email addresses from the list in Java. Whether through basic string manipulation or by utilizing Java Streams, the target is to discover and eliminate duplicates using the domain and username.

最后,我们介绍了用 Java 从列表中分离出独占电子邮件地址的各种技术。无论是通过基本的字符串操作还是利用 Java Streams,其目标都是利用域名和用户名发现并消除重复。

As always, the complete code samples for this article can be found over on GitHub.

与往常一样,本文的完整代码示例可在 GitHub 上找到