0

I have list of objects in an arraylist and I need to compare every objects with other objects available in the arraylist; For Example:

Class Employee {
    private String empname;
    private Long empid;
    private boolean empsex;

    public String getEmpname() {
        return empname;
    }
    public void setEmpname(String empname) {
        this.empname = empname;
    }
    public Long getEmpid() {
        return empid;
    }
    public void setEmpid(Long empid) {
        this.empid = empid;
    }
    public boolean isEmpsex() {
        return empsex;
    }
    public void setEmpsex(boolean empsex) {
        this.empsex = empsex;
    }
}

public list<Employee> getEmpList() {
    List<Employee> empList = new ArrayList<Employee>();
    Employee emp =  new Employee();
    for(...) {
        //insert values to emp object for n number of times;
    }
    empList.add(emp); //add emp.object to empList;
    return empList;
}

Now while inserting these values to UI; Need to compare objects in the list; where any two or more objects matches with each other or not?

Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
John Smith
  • 1,095
  • 2
  • 11
  • 24

4 Answers4

0

Based on the assumption that you want to eliminate the duplicates from the list and do not show duplicates on GUI.

Use Set collection, it automatically takes care of duplicates.

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

Override equals() and hashcode() methods.

References:

Community
  • 1
  • 1
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
0

You could override equals method for that class to compare the objects of the same class the way you want.

Default equals method:

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String) anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                        return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

Link about overriding equals:

http://javarevisited.blogspot.com/2011/02/how-to-write-equals-method-in-java.html

msmolcic
  • 6,407
  • 8
  • 32
  • 56
0

One simple method of doing this is to just do a double for loop.

public static List<Employee> getList(List<Employee> oldList)
{
    List<Employee> empList = new ArrayList<Employee>();
    for (int i = 0; i < oldList.size; i++)
    {
        for (int j = 0; j < oldList.size; j++)
        {
            //compare oldList.get(i) with oldList.get(j)
            //if match, set some boolean
        }
    //if duplicate found, delete one copy, or add one to new list, etc
    }

This allows you to go through each element in the outer loop, and compare to every other element in the inner loop.

Drifter64
  • 1,103
  • 3
  • 11
  • 30
0

I guess employee id (empid) is unique for each emplyee, yes?

If so, use a hash instead where empid is the key and employee object is the value.

Map<Long, Employee> empmap = new HashMap<Long, Employee>();
empmap.put(currentEmployee.getEmpid(), currentEmployee)
Fredrik
  • 10,626
  • 6
  • 45
  • 81
  • For those unique ID's; How to check Employee object like name and emp-sex are same? Having two loops? – John Smith Feb 12 '14 at 17:45
  • @JohnSmith Why would you compare those? You could have two different employees with the same name. No, make sure you have one unique identifier and compare with that only. – Fredrik Feb 13 '14 at 08:19