"重复" ArrayList中的条目?

时间:2015-03-11 15:23:27

标签: java inheritance arraylist duplicates

我有这个课程将填写一个列表,其中包含所有在数组中预先制作的员工。我可以在员工中填充一个ArrayList,但唯一的问题是我得到了一些" Duplicate"条目,我使用报价因为它们不完全相同,但他们可以共享相同的名称或员工编号,但可能没有相同的雇用年份或工资等。

继承员工班:

public class Employee {

public String EmployeeName;
public String EmployeeNumber;
public int hireyear;
public double WeeklyEarning;

public Employee()
{
    EmployeeName = null;
    EmployeeNumber = null;
    hireyear = 0;
    WeeklyEarning = 0;

}

public static final String[] Empnum = new String[] {
        "0001-A", "0002-B","0003-C","0004-D","0002-A",
        "0003-B","0004-C","0005-D","0011-A", "0012-B",
        "0013-C","0014-D","0121-A", "0122-B","0123-C",
        "0321-A", "0312-B","1234-D","4321-C","1122-D"};

public static final String[] Ename = new String[] {
        "Josh", "Alex", "Paul", "Jimmy", "Josh", "Gordan", "Neil", "Bob",
        "Shiv", "James", "Jay", "Chris", "Michael", "Andrew", "Stuart",
        "Dave","Benjamin","Dan","Brian","Michelle"};

public String getEmployeeName()
{
    return this.EmployeeName;
}

public String getEmployeeNumber()
{
    return this.EmployeeNumber;
}

public int gethireyear()
{
    return this.hireyear;
}

public double getWeeklyEarning()
{
    return this.WeeklyEarning;
}

public String setEmployeeName(String EName)
{
    return this.EmployeeName = EName;
}

public String setEmployeeNumber(String ENumber)
{
    return this.EmployeeNumber = ENumber;
}

public int setEmployeehireyear(int Ehireyear)
{
    return this.hireyear = Ehireyear;
}

public double setEmployeeweeklyearning(double Eweeklyearning)
{
    return this.WeeklyEarning = Eweeklyearning;
}

public String toString(){
    String data = "\n Employee Name : " + EmployeeName + " \n Employee Number: " + EmployeeNumber + " \n Hire Year : " + hireyear + "\n Weekly Earnings : " + WeeklyEarning;
    return data;
}

public boolean equals(Object o){
    if(this == null){
        return false;
    }
    if(this == o){
        return true;
    }
    if(!(o instanceof Employee)){
        return false;
    }

    Employee temp = (Employee) o;
    if(this.getEmployeeName().equals(temp.getEmployeeName())){
        return true;

    }
    if(this.getEmployeeNumber().equals(temp.getEmployeeNumber())){
        return true;

    }
    if(this.gethireyear() == temp.gethireyear()){
        return true;
    }
    if(this.getWeeklyEarning() == temp.getWeeklyEarning()){
        return true;
    }
    return false;

}

}

继承将填充列表的generateList方法:

public ArrayList<Employee> generateEmpList(){
    empList = new ArrayList <Employee>();
    Random empPicker = new Random();

    for(int i = 0; i < 20; i++){
        int id = empPicker.nextInt(20);
        if(id < 12) // roll for production worker
        {

            //System.out.println("Adding Production Worker");
            ProductionWorker temp = new ProductionWorker();
            temp = temp.generateProductionWorker();
            prodWorker = temp;
            empList.add(prodWorker);

        }
        else //roll for Shift supervisor
        {
            //System.out.println("Adding Shift supervisor");
            ShiftSupervisor supervisor = new ShiftSupervisor();
            supervisor = supervisor.generateShiftSupervisor();
            shiftWorker = supervisor;
            empList.add(shiftWorker);
        }
    }
    Iterator iterator = empList.iterator();
    while (iterator.hasNext()) {
        System.out.println("");
        System.out.println(iterator.next());

    }
    return empList;
}

并且可能有用的是&#34; generateProductionWorker()&#34;和shiftSupervisor方法 - 只有在prod worker方法之后才能让它变得简短,因为它们基本相同:

public ProductionWorker generateProductionWorker(){
    Random rng = new Random();
    int numberOfEmployeeNames = Ename.length;
    ProductionWorker tempPworker = new ProductionWorker();

    String employeeName = Ename[rng.nextInt(numberOfEmployeeNames)];
    tempPworker.setEmployeeName(employeeName);

    int numberOfEmployeeNumbers = Empnum.length;
    String employeeNumber = Empnum[rng.nextInt(numberOfEmployeeNumbers)];
    tempPworker.setEmployeeNumber(employeeNumber);

    int yearHired = rng.nextInt(35) + 1980;
    tempPworker.setEmployeehireyear(yearHired);

    double weeklySalary = rng.nextInt((100) * 100);
    tempPworker.setEmployeeweeklyearning(weeklySalary);

    int hourlyRate = rng.nextInt(20) + 10;
    tempPworker.setHourlyRate(hourlyRate);

    return tempPworker;

}

我确定我错过了一些微不足道的事情,但是当我有20个姓名和号码的列表时,为什么我会得到类似的条目呢?

例如: empname - josh empnum - 0000-A hireyear - 1994 薪水 - 40,000

empname - josh empnum - 0000-A hireyear - 1999 薪水 - 60,500

任何建议都会有所帮助,谢谢!

2 个答案:

答案 0 :(得分:2)

查看Employee类中的equals方法。如果它们的名称相同,则返回true,这意味着它们是等于的。其他属性也是如此。您必须替换if语句。

答案 1 :(得分:1)

我同意Georgi,你等于方法是罪魁祸首。

目前,在读取

行的第一个if语句后返回true
if(this.getEmployeeName().equals(temp.getEmployeeName())){
    return true;
}

因为它是return语句,所以它会阻止方法继续执行其他语句。你可以试试这个:

    public boolean equals(Object o){
        if(this == null){
            return false;
        }
        if(this == o){
            return true;
        }
        if(!(o instanceof Employee)){
           return false;
        }
        //set all the elements in the array to false and change to true when true.
        boolean [] doesItMatch = new boolean[4];
        doesItMatch[0] = false;
        doesItMatch[1] = false;
        doesItMatch[2] = false;
        doesItMatch[3] = false;

        Employee temp = (Employee) o;
        if(this.getEmployeeName().equals(temp.getEmployeeName())){
            doesItMatch[0] = true;
        }
        if(this.getEmployeeNumber().equals(temp.getEmployeeNumber())){
            doesItMatch[1] = true;
        }
        if(this.gethireyear() == temp.gethireyear()){
            doesItMatch[2] = true;
        }
        if(this.getWeeklyEarning() == temp.getWeeklyEarning()){
            doesItMatch[3] = true;
        }
        int check = 0;
        //Now that you have checked all the values, check the array. Using a simple counter. 
        for(int i = 0; i < doesItMatch.length; i++){
            if(doesItMatch[i]){
                check++;
            } else {
                check--;
            }
        }
        //The counter should be 4 when the if statements above are all true. Anything else is false.
        if(check == 4){
            return true;
        } else {
            return false;
        }
    }

此方法现在检查Employee类中的每个属性。 (名称,数量,雇用年份等。如果为类创建更多属性,则可以轻松地向阵列添加更多元素,请确保将它们设置为false。) 希望这可以帮助 如果扩展Employee类,这也需要一些维护,因此您可能希望找到一种方法使自己更容易一些。

相关问题