tags:

views:

86

answers:

4

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?

A: 

There are several possibilities, what kind of list do you use?

lain
A: 

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
+2  A: 

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

Upul
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
but i also want to sort the list later on
akshay
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
+2  A: 

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();
    }
}
aioobe
... and hashCode()! :)
Bart Kiers
Ah, right, that's a good idea :)
aioobe
@aioobe, I'd check for `null` as well in `equals(...)` and perhaps add `@Override` annotations before `equals(...)` and `hashCode()`.
Bart Kiers
is it necessary to overide hashcode too? or only overriding equals will do?
akshay
@akshay, not necessary, but it is highly recommend. See: http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java
Bart Kiers
@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
@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