If, given an array of objects, such as:
ArrayList<Person> people = new ArrayList<>(Arrays.aslist(
new Person("Victoria", 25, "Firefighter"),
new Person("Grace", 27, "Footballer"),
new Person("Samantha", 25, "Stock Broker"),
new Person("Victoria", 23, "Poker Player"),
new Person("Jane", 27, "Footballer"),
new Person("Grace", 25, "Security Guard"));
How can one remove any objects that don't have a unique attributes, whilst leaving only one. This could be as simple as duplicate names, which would leave:
Person("Victoria", 25, "Firefighter"),
Person("Grace", 27, "Footballer"),
Person("Samantha", 25, "Stock Broker"),
Person("Jane", 27, "Footballer")
Or more complex, such as jobs that start with the same letter, and the same age:
Person("Victoria", 25, "Firefighter"),
Person("Grace", 27, "Footballer"),
Person("Samantha", 25, "Stock Broker"),
Person("Victoria", 23, "Poker Player"),
So far, the best I've come up with is:
int len = people.size();
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++)
if (function(people.get(i), people.get(j))) {
people.remove(j);
j--;
len--;
}
}
With "function" checking if the entries are considered "duplicates"
I was wondering if there's a library that does just this, or if you could somehow put this in a lambda expression