-5
class Employee implements Comparable<Employee> 
{

    public Employee(int id, String name,int salary ,String designation)
    {..}

//getters and setters

}
 public class TestEmployeeSort
{
     public static void main(String[] args) 
     {
         List<Employee> col = new ArrayList<Employee>();
             Employee one =  new Employee(**2**, "rahul",100, "bc");
         Employee oone =  new Employee(**2**, "sfdful",1300, "bdfsc");
         col.add(one);
         col.add(oone);

     }
}

here i made a program where 4 fields in object are passed named id, name, salary and designation now from the arraylist objects i want to pick up one entity of object out of 4 used named "id" and want to make it unique so that no object with duplicate id can be inserted ...(similar to working of set but don't want to use set here) how can it be done .....i tried making a method in constructor and passing "this.id" in a set to check duplicates but still it is not working???

4 Answers4

3

Sounds like you want to use Set, and not List:

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.

MByD
  • 135,866
  • 28
  • 264
  • 277
3

You could use a java.util.Set implementation instead of a List to ensure that there are no duplicates in it. This depends on equals, which must be implemented to compare the id.

If you'd like to have something sorted, you may use the TreeSet implementation.

Zeemee
  • 10,486
  • 14
  • 51
  • 81
  • i get it but...my question is how can i pick one entity out of 4 of a object – ravindersangwan May 17 '12 at 12:35
  • Maybe it would help to use a key/value structure like `HashMap`. Your key is `id` and your value is the `Employee` object. It would ensure that there are no duplicate keys (IDs) and you could pick up an `Employee` by `id`. – Zeemee May 18 '12 at 07:29
2

I would suggest to create a class to extend ArrayList, and override the add method, so when you try to insert, first call the contains method and return false if it is already in the list:

@Override
public boolean add(E e){
 if(super.contains(e){
    return false;
 }else{
    super.add(e);
 }
return true;
}
Eugenio Cuevas
  • 10,858
  • 3
  • 29
  • 51
0

If you're dead set on using ArrayList, maybe you should extend it and implement your own add function.

Nathan Dortman
  • 406
  • 1
  • 7
  • 20
  • can't we just pick up one field from arraylist object??? – ravindersangwan May 14 '12 at 08:46
  • I'm not sure as to what you mean when you say "pick up one field". From what I understand, you want to have a `List` that has some kind of **Unique** Key member/property and you don't want duplicated in **that** field. The default implementation of `ArrayList` doesn't support this functionality, so if you want that functionality you either have to implement your own `List` or use a different class such as `Set`. – Nathan Dortman May 14 '12 at 11:24