0

I have an Object Person(id, name).

public class Person {


/** Personid. **/
private Long personId;

/** Person Adresse. **/
private String adresse

// Getters, Setters

How can i remove duplicate person (who have same id) using CollectionUtils ? For example Person1(10, aaaa), Person2(10, bbbb), Person3(20, cccc) Result => Person1(10, aaaa), Person3(30, cccc)

EDIT :

This Solution work using Set and overrinding equals and hashcode:

List<Person> oldPerson = new ArrayList<>();
//oldPerson.add ...

Set<Person> newPerson = new HashSet<>(oldPerson);

List<Person> theRightPerson = new ArrayList<>(newPerson);

The solution that i m looking for is something like :

 List<Person> theRightPerson = (List<Person>) CollectionUtils.collect(oldPerson, new Transformer() {

        @Override
        public Object transform(Object input) {
            // TODO Auto-generated method stub
            return null;
        }
    })
Anarki
  • 373
  • 5
  • 20

1 Answers1

0

Overriding equals and hashcode methods and using HashSet would be easier to do. If you can't edit the source of the class, then you need to iterate over the list and compare each item based on criteria.

How to remove duplicate objects in a List<MyObject> without equals/hashcode?

Community
  • 1
  • 1
cerberus
  • 531
  • 4
  • 14