-2

I have two objects as Employee and Person. Both of them have firstName and lastName. I don't want to add object(either Employee or Person) to the list if firstName,lastName for either Person and Employee has already been added to the list.

Use equals and hashcode to do this...

Edit:I cannot use Set or any other collection, and have to use equals and hashcode to achieve this.

6 Answers6

1

For equals/hashcode example you can have a look at this question. As for adding to List, there are two options:

  1. Check if List contains the object with List.contains method every time before you add an element to it
  2. Use LinkedHashSet, which will save the order of added elements and then return new ArrayList<Employee>(employeeLinkedHashSet);
Community
  • 1
  • 1
potato
  • 471
  • 2
  • 13
0

Since you want to couple the identities of Employee and Person, this can't be achieved trivially, except if you are prepared to

  1. have Employee extends Person and
  2. have all Employees equal as soon as they equal as Persons, regardless of any further properties they may have.

If these constraints are acceptable to you, then just implement Person#equals and Person#hashCode to involve firstName and lastName and use a Set to eliminate duplicates.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

Use the to avoid duplicates read more in the Java API Doc

Waleed Almadanat
  • 1,027
  • 10
  • 24
0
  1. Implement a List that wraps an existing List implementation like ArrayList
  2. Delegate all method calls to the wrapped list
  3. Implement add and addAll so that they do not add the values if they're already in the list.

Note, that this implementation would be inconsistant with the List interface, because user expect that a call to add really adds the object to the collection (size increases by one).

It would be much better to either use a Set implementation (avoids duplicates but does not preserve insertion order) or check for duplicates before you add:

 List<Person> persons = getPersonsFromSomewhere();
 Person person = getAPersonThatShallBeAddedIfNotDuplicate();
 if (!persons.contains(person)) {
    persons.add(person);
 }
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
0

Use a Set and not a list. If you need a list you can call Set.toArray() and pass that to a List constructor.

Have you Employee and Person classes implement Comparable<T>. Making Employee a subclass of Person might make this easier.

Colin D
  • 5,641
  • 1
  • 23
  • 35
0

I suspect you should be using a map if your have some fields which should be unique. You can use a custom class which is a compound key, or you can combine the Strings into a single string using a seperator.

Map<String, Employee> employeeMap = ....

String fullname = employee.firstName + "~" + employee.lastName;
Employee employee = employeeMap.get(fullname);
if (employee == null) {
   // not in the map so add it.
   employeeMap.put(fullname, employee);
} else {
   // employee is already in the collection.
   // alter it or produce a warning
}

Note: String already has equals and hashCode methods so you don't need to add them.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130