以下代码使用 List 检查重复项
public List<Person> getGroupMembers() {
final List<Person> persons = new ArrayList<Person>(groupMembers.size());
for (GroupMember member : groupMembers) {
if (member.getPerson() != null && !member.getPerson().isDeleted()) {
persons.add(member.getPerson());
for (int i = 0; i < persons.size(); i++) {
for (int j = i + 1; j < persons.size(); j++) {
if (persons.get(i).equals(persons.get(j))) {
persons.remove(j);
i = 0;
}
}
}
}
}
return persons;
}
**Now for checking the duplicity I have to use Set Collection and convert it to List
为此,我进行了如下更改**
public List<Person> getGroupMembers() {
final List<Person> persons = new ArrayList<Person>(groupMembers.size());
final HashSet<Person> setPersons = new HashSet<Person> ();
for (GroupMember member : groupMembers) {
if (member.getPerson() != null && !member.getPerson().isDeleted()) {
setPersons.add(member.getPerson());
persons.addAll(setPersons);
}
}
return persons;
}
但是上面的代码没有按预期工作,请建议