我有 2 个自定义对象的 ArrayList。我想从 ArrayList 中删除重复的条目。
这些对象具有三个字段:fName、lName 和 id。如果 id 出现多次,我想从两个列表中删除。
我怎样才能做到这一点?
我可以合并两个列表并删除 2 个重复条目。
如果要合并:只需将两个列表的内容复制到地图。然后你不再有重复项了(但你失去了你的实际顺序):
Map<Integer, MyObject> temp = new HashMap<Integer, MyObject>();
for (MyObject obj:firstList) {
temp.put(obj.getId(), obj);
}
for (MyObject obj:secondList) {
temp.put(obj.getId(), obj);
}
List<MyObject> result = new ArrayList<MyObject>(temp.values());
如果您的类正确实现了equals
andhashCode
方法,请将列表转换为 aHashSet
以消除重复项。构造HashSet<T>
函数接受 aCollection<T>
所以你应该很高兴。
如果您需要一些自定义比较器功能(例如在您的情况下仅比较的功能id
),请在Comparator<T>
创建TreeSet
. 总而言之,只需创建一个比较器,它比较id
两个对象的 s 并将其传递给TreeSet
构造函数。然后将两个列表中的项目添加到该集合中将负责消除重复项。就像是:
public class Test {
public static void main(String[] args) {
Person p1 = new Person("first", "id1");
Person p2 = new Person("dummy", "id1"); // same id as above
Person p3 = new Person("second", "id2");
Person p4 = new Person("third", "id1");
List<Person> asList = Arrays.asList(p1, p2, p3, p4);
CustomComparator comparator = new CustomComparator();
TreeSet<Person> ts = new TreeSet<Person>(comparator);
TreeSet<Person> duplicates = new TreeSet<Person>(comparator);
for (Person p : asList) {
if (ts.contains(p) || duplicates.contains(p)) {
duplicates.add(p);
ts.remove(p);
} else {
ts.add(p);
}
}
System.out.println(ts);
}
}
class Person {
public Person(String name, String id) {
super();
this.name = name;
this.id = id;
}
public String name;
public String id;
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Person [id=");
builder.append(id);
builder.append(", name=");
builder.append(name);
builder.append("]");
return builder.toString();
}
}
class CustomComparator implements Comparator<Person> {
@Override
public int compare(Person o1, Person o2) {
return o1.id.compareTo(o2.id);
}
}
Use Set for this purpose.
Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element.
HashSet<Integer> list_1_ids = new HashSet<Integer>();
HashSet<Integer> list_2_ids = new HashSet<Integer>();
for (CustomObject x : list1) list_1_ids.add(x.id);
for (CustomObject x : list2) list_2_ids.add(x.id);
HashSet<Integer> both_ids = list_1_ids;
both_ids.retainAll(list_2_ids);
List<CustomObject> pruned_list_1 = new ArrayList<CustomObject>();
for (CustomObject x : list1) if (!both_ids.contains(x.id)) pruned_list_1.add(x);
List<CustomObject> pruned_list_2 = new ArrayList<CustomObject>();
for (CustomObject x : list2) if (!both_ids.contains(x.id)) pruned_list_2.add(x);