检测包含多个员工对象的arraylist中的重复电子邮件ID

时间:2017-10-15 18:46:32

标签: java collections

假设我有一个arraylist,我正在向arraylist动态添加多个员工对象。 Employee对象具有Id,name,email等字段。我的要求是当我向arraylist添加一个雇员对象时。假设具有已经存在另一个对象的电子邮件的对象被添加到arraylist。然后它不应该允许将当前对象添加到arraylist或显示一些错误消息。在Collection模块中是否有任何方法可以以最短的方式实现此事件。

4 个答案:

答案 0 :(得分:2)

如果我的问题正确,你需要根据emailaddress属性避免重复员工对象。我建议使用Sets而不是arraylist。

这就是你如何使用覆盖等于和哈希码的集合。

 package com.test.testing;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );

        Employee employee = new Employee("anilhk@gmail.com", "1");
        Employee employee2 = new Employee("abc@gmail.com", "2");
        Employee employee3 = new Employee("anilhk@gmail.com", "3");

        List<Employee> empList = new ArrayList<Employee>();
        empList.add(employee);
        empList.add(employee2);
        empList.add(employee3);

        System.out.println("Employee List " +empList);

        Set<Employee> empSet = new HashSet<Employee>();

        for (Employee emp : empList) {

            if (empSet.contains(emp)) {
                System.out.println("Employee with employee email " +emp.getEmailAddress() + " and employee id " +emp.getId() +" already exists");
            } 
            else {
                empSet.add(emp);
            }

        }

        System.out.println(empSet);
    }

    private static class Employee {

        private String emailAddress;
        private String id;


        @Override
        public String toString() {
            return "Employee [emailAddress=" + emailAddress + ", id=" + id + "]";
        }

        public Employee(String emailAddress, String id) {
            this.emailAddress = emailAddress;
            this.id = id;
        }

            public String getEmailAddress() {
            return emailAddress;
        }

        public void setEmailAddress(String emailAddress) {
            this.emailAddress = emailAddress;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((emailAddress == null) ? 0 : emailAddress.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (!(obj instanceof Employee)) {
                return false;
            }
            Employee other = (Employee) obj;
            if (emailAddress == null) {
                if (other.emailAddress != null) {
                    return false;
                }
            } else if (!emailAddress.equals(other.emailAddress)) {
                return false;
            }
            return true;
        }





    }

}

输出

  Hello World!
Employee List [Employee [emailAddress=anilhk@gmail.com, id=1], Employee [emailAddress=abc@gmail.com, id=2], Employee [emailAddress=anilhk@gmail.com, id=3]]
Employee with employee email anilhk@gmail.com and employee id 3 already exists
[Employee [emailAddress=anilhk@gmail.com, id=1], Employee [emailAddress=abc@gmail.com, id=2]]

employee3与雇员具有相同的电子邮件地址,因此从列表中排除。

HTH

答案 1 :(得分:1)

您必须编写一个函数,该函数将遍历数组列表的所有节点,并检查您当前的电子邮件地址是否存在。如果它存在则返回false或true并显示消息。

答案 2 :(得分:1)

// Use LinkedHashMap to keep insertion order
Map<String, Employee> employees = new LinkedHashMap<>();

// v1: add new employee with unique email
employees.putIfAbsent(employee.getEmail(), employee);

// v2: add new employee and show message for duplication email
if(employees.containsKey(employee.getEmail()))
    System.out.println("Email " + employee.getEmail() + " duplication");
else
    employees.put(employee.getEmail(), employee);

// get all employees in order they were added
List<Employee> res = new ArrayList<>(employees.values());

答案 3 :(得分:0)

更有效的方法是保留一组单独的电子邮件(设置&lt; String&gt; emailSet ),并在每次将员工添加到列表之前进行检查:

if (!emailSet.contains(emp.getEmail()) {
    employeeList.add(emp);
    emailSet.add(emp.getEmail());
} else {
    //show error message
}