2

i have a list of employees.My employee class is as follows

public class Employee{
  int empid;
  String name;
  ...
}

now i want to remove a employee from list whose empid is 5.One way to do is to iterate the list and check if empid == 5.Is there any other way by which i can do it?

Also is want my list to contain employees with unique empid.Any attempt made to add employees with duplicate empid should throw an exception.How to do this?

akshay
  • 553
  • 2
  • 5
  • 9
  • just remove your Employee by list.remove( object ), where object is your employee to delete. Or you only have the id of your employee? – crusam Aug 13 '10 at 10:41

3 Answers3

2

Instead of List< Employee>, use Set< Employee>. Don't forget to override hashCode() and equals() methods of your Employee class.

Upul Bandara
  • 5,769
  • 4
  • 32
  • 59
  • It should be noted that this suggestion presupposes that the OP doesn't need to store one employee multiple times, and that the order of the employees is irrelevant. – aioobe Aug 13 '10 at 10:56
  • but i also want to sort the list later on – akshay Aug 13 '10 at 10:56
  • Then you either need to use a class implementing SortedSet (TreeSet for instance) or stick with the List and go for one of the other options presented in the other answers. – aioobe Aug 13 '10 at 11:13
2

If the order of the employees is of relevance (or if you need to be able to let one employee be represented multiple times) you need to store them in a list. (Otherwise a Set would suffice.)

I would let Employee override the equals method and use List.remove(Object o).

From the API docs of List:

boolean remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present (optional operation). If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).

Concretely, you could do something like

public class Employee{

    int empid;
    String name;

    public boolean equals(Object o) {
        if (o == null || !(o instanceof Employee))
            return false;
        Employee e = (Employee) o;
        return empid == e.empid && name.equals(e.name);
    }

    public int hashCode() {
        return empid ^ name.hashCode();
    }
}
Bart Kiers
  • 153,868
  • 34
  • 276
  • 272
aioobe
  • 383,660
  • 99
  • 774
  • 796
  • @aioobe, I'd check for `null` as well in `equals(...)` and perhaps add `@Override` annotations before `equals(...)` and `hashCode()`. – Bart Kiers Aug 13 '10 at 11:18
  • is it necessary to overide hashcode too? or only overriding equals will do? – akshay Aug 13 '10 at 11:25
  • @akshay, not necessary, but it is highly recommend. See: http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java – Bart Kiers Aug 13 '10 at 11:50
  • @Bart K, you make very good points. Whenever you find room for improvement in any of my posts, don't hesitate to edit. :) I would like to add though, that I really like minimalistic and concise examples that have as few code-lines "off-topic" as possible... – aioobe Aug 13 '10 at 12:08
  • @aioobe, okay, will do that! :) Yes, the `@Override` can be omitted in examples here, but the check for `null` ought to be in there since certain collections can contain `null`'s. So I added that to it. – Bart Kiers Aug 13 '10 at 12:27
0

For the first part to your question, you can call remove() passing in an Employee object whose equals() method returns true for an Employee of id 5.

For your second part, instead of a List, a Set guarantees not to have any duplicates. Is it necessary for your collection to be a list?

Noel M
  • 14,881
  • 7
  • 35
  • 45