Remove from ArrayList if one value of objects repeats

tomasz pawlak :

How to remove duplicate object from ArrayList, but only if one specific value from object repeats with another object?

For example: I have class named Person with fields:

private String city;
private String firstName;
private String lastName;
private Long magicNumber;

I want to remove "older" Person with same "magicNumber" as the new One and keep him in ArrayList.

Eritrean :

Using streams :

Collection<Person> filterd = persons.stream()
            .collect(Collectors.toMap(
                    Person::getMagicNumber, p -> p, (p1, p2) -> p2))
            .values();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=25239&siteId=1